Question

In: Computer Science

Write a c++ program to convert any decimal number to either binary base  or Hex base...

Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings:


Convert 15 from decimal to binary. 
Convert 255 from decimal to binary.
Convert BAC4 from hexadecimal to binary
Convert DEED from hexadecimal to binary. 
Convert 311 from decimal to hexadecimal.
Convert your age to hexadecimal.


Solutions

Expert Solution

// C++ program to convert Decimal number to binary or hexadecimal

#include <iostream>

#include <sstream>

using namespace std;

int main() {

       int decimalNumber;

       string convertedOutput;

       int choice;

       // input the decimal number

       cout<<"Enter the decimal number to convert :";

       cin>>decimalNumber;

       // input the choice of conversion

       cout<<"Enter the choice of conversion : 1. Binary 2. Hexadecimal : ";

       cin>>choice;

       cout<<"Decimal : "<<decimalNumber;

       convertedOutput = "";

       int rem;

       int temp = decimalNumber;

       if(choice == 1) // convert decimal to binary

       {

             // loop that continues till temp > 0

             while(temp > 0)

             {

                    rem = temp%2; // get the remainder left after multiplying temp by 2

                    temp = temp/2; // get the value left after multiplying temp by 2

                    // convert the rem to string

                    stringstream ss;

                    ss<<rem;

                    // add the rem to output

                    convertedOutput = ss.str() + convertedOutput;

             }

             cout<<" Binary : ";

       }else // convert decimal to hexadecimal

       {

             // loop that continues till temp > 0

             while(temp > 0)

             {

                    rem = temp%16; // get the remainder left after multiplying temp by 16

                    temp = temp/16; // get the value left after multiplying temp by 16

                    // if rem < 10, then add the string equivalent to output

                    if(rem < 10)

                    {

                           stringstream ss;

                           ss << rem;

                           convertedOutput = ss.str() + convertedOutput;

                    }else // if rem >=10

                    {

                           // get the corresponding letter of the hexadecimal and add it to output

                           convertedOutput = (char)('F'-(15-rem)) + convertedOutput;

                    }

             }

             cout<<" Hexadecimal : ";

       }

       // output the converted value

       cout<<convertedOutput<<endl;

       return 0;

}

//end of program

Output:

Convert 15 from decimal to binary.

Convert BAC4 from hexadecimal to binary (decimal equivalent of BAC4 = 47812)

Convert 311 from decimal to hexadecimal.


Related Solutions

c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as...
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as 0x##(ex 0x0A, 0xFF) Binary Hexadecimal Decimal 0001-1011 0x 0000-1000 0000-0100 0000-1001 0001-1111 1001-1001 0111-1010 1100-0010 1110-0101 1000-1010 0011-0100 0001-1001 0100-0011 1111-1111 1110-0111 0001-0010 0100-1000 0100-1110 1001-0001 0110-1100 Name: Convert the following hexadecimal values to binary and decimal Write binary numbers as 0000-0000 Hexadecimal Binary Decimal 0xf1 0xac 0x56 0x6c 0x32 0x30 0x05 0x28 0xf0 0x07 0x42 0xb9 0x6d 0x2f 0x71 0x0e 0x2d 0xfb 0xba...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
How to convert binary to hex?
How to convert binary to hex?
Convert the following unsigned numbers to the requested form: 01100001 binary to: hex, and also decimal...
Convert the following unsigned numbers to the requested form: 01100001 binary to: hex, and also decimal Hex: Decimal: b) 136 decimal to: hex, and also binary Hex: Binary:
Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number to fractional binary representation. 1. 103.5625
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT