Question

In: Computer Science

int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...

int bintodec(string); // converts a binary number (represented as a STRING) to decimal
int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal
string dectobin(int); // converts a decimal number to binary (represted as a STRING)
string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING)


//the addbin and addhex functions work on UNsigned numbers

string addbin(string, string); // adds two binary numbers together (represented as STRINGS)
string addhex(string, string); // adds two hexadecimal numbers together (represented as STRINGS)


int main()
{
   cout<<"SIGNED binary 10011 = SIGNED decimal "<<bintodec("10011")<<endl;
   cout<<"SIGNED binary 01110 = SIGNED decimal "<<bintodec("01110")<<endl<<endl;
  
   cout<<"SIGNED hexadecimal A1 = SIGNED decimal "<<hextodec("A1")<<endl;
   cout<<"SIGNED hexadecimal 3C = SIGNED decimal "<<hextodec("3C")<<endl<<endl;
  
   cout<<"SIGNED decimal 25 = SIGNED binary "<<dectobin(25)<<endl;
   cout<<"SIGNED decimal -18 = SIGNED binary "<<dectobin(-18)<<endl<<endl;
  
   cout<<"SIGNED decimal 28 = SIGNED hexadecimal "<<dectohex(28)<<endl;
   cout<<"SIGNED decimal -15 = SIGNED hexadecimal "<<dectohex(-15)<<endl<<endl;
  
   cout<<"binary 1101 + 1000 = "<<addbin("1101", "1000")<<endl;
   cout<<"binary 11000 + 1011 = "<<addbin("11000", "1011")<<endl<<endl;
  
   cout<<"hexadecimal A4 + A5 = "<<addhex("A4", "A5")<<endl;
   cout<<"hexadecimal 2B + C = "<<addhex("2B", "C")<<endl<<endl;
      
   return 0;
  
}


int bintodec(string bin)
{

/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
  
}

int hextodec(string hex)
{

/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
  
  
}

string dectobin (int dec)
{
  
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
  
  
  
}

string dectohex(int dec)
{
  
/*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
  
  
}

string addbin(string bin1, string bin2)
{
  
   /*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
  
  
}

string addhex(string hex1, string hex2)
{
  
   /*
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
##### IMPLEMENT THIS #####
*/
  
  
  
}

Solutions

Expert Solution

int bintodec(string bin)
{

            int decimal, base, remainder;
            decimal = 0;
            base = 1;           
            int x = atoi(bin.c_str());             //String to integer conversion
            while (x > 0)

                        {

                       remainder = x % 10;

                                    decimal = decimal + remainder * base;

                       base = base * 2;

                       x = x / 10;

           }

            return x;        

}

int hextodec(string hex)

       {

              char arr[100];

              int len, i, test, length, decimal, base = 16;

              i = 0;

              strcpy(arr, hex.c_str());         //string to character array

              len = strlen(hex);

              len = len - 1;

              length = len;

              while (len > 0)

              {

                     test = arr[i];

                     if (test >= 48 && test <= 57)   // test Between 0-9

                     {

                           decimal += (test - 48) * pow(base, length - i - 1);

                     }

                     else if ((test >= 65 && test <= 70))   // test Between A-F

                     {

                           decimal += (test - 55) * pow(base, length - i - 1);

                     }

                     else if (test >= 97 && test <= 102)   // test Between a-f

                     {

                           decimal += (test - 87) * pow(base, length - i - 1);

                     }

                     else

                     { cout << "Not Hex value"; }

                     len = len -1;

                     i++ ;

              }

              return decimal;

       }

string dectobin(int dec)

{

       int remainder;

       if (dec <= 1)

       {

              cout << dec;

              return;

       }

       remainder = dec % 2;

       dectobin(dec / 2);

       cout << remainder;

}

string dectohex(int dec)          //use _itoa(), if itoa() does not work;

{                                       

       char buffer[50];

       itoa(dec, buffer, 16);

       cout << buffer;

}

string addbin(string bin1, string bin2)

{

       int num1, num2, result;

       num1 = bintodec(bin1);

       num2 = bintodec(bin2);

       result = num1 + num2;

       dectobin(result);

}

string addhex(string hex1, string hex2)

{

       int num1, num2, result;

       num1 = hextodec(hex1);

       num2 = hextodec(hex2);

       result = num1 + num2;

       dectohex(result);

}


Related Solutions

3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int))....
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int)). It should expect to be passed a natural number (such as 12345) and return the corresponding words (i.e., “twelve thousand three hundred forty-five”). Since Python integers can grow arbitrarily large, your code should be flexible, handling at least 20 digits (realistically, it’s just as easy up to 30 digits). Spell everything correctly, and use correct punctuation (hyphens for forty-five and thirty-seven, no commas or...
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int))....
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int)). It should expect to be passed a natural number (such as 12345) and return the corresponding words (i.e., “twelve thousand three hundred forty-five”). Since Python integers can grow arbitrarily large, your code should be flexible, handling at least 20 digits (realistically, it’s just as easy up to 30 digits). Spell everything correctly, and use correct punctuation (hyphens for forty-five and thirty-seven, no commas or...
Write a program that inputs a string that represents a binary number. The string can contain...
Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console. Examples of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT