C++ Program to Convert Number Hexadecimal to Binary

Question : Write a C++ C++ Program to Convert Number Hexadecimal  to Binary?

Explain: C++ Language Question about conversion Number Hexadecimal to Binary with input from user.

The Hexadecimal number is the base-16 number system, and uses the digits 0 to 9 and A,B,C,D,E,F .

Formula:
Table of Hexadecimal to Binary 

Hex=Binary code
0=0000
1=0001     
2=0010       
3=0011      
4=0100   
5=0101   
6=0110     
7=0111
8=1000
9=1001     
A=1010       
B=1011      
C=1100   
D=1101   
E=1110     
F=1111


Solution:

/************************************************************ 
     C++ Program for Convert Number Hex to Binary
 *************************************************************
***************** By www.cpplanguage.com *********************/

using namespace std;
int main()
{
    char hexNumber[500];
    long int c=0;
    cout<<"Enter you Hexadecimal number: ";
// getting input octal number
    cin>>hexNumber;
    cout<<hexNumber<<" = "<<"binary ";
    for(c=0;c<hexNumber[c];c++)
    {
// applying formula  Hexadecimal value to binary , also small letters a,b,c,d,e,f because same value as upper case
        switch(hexNumber[c])
          {
             case '0': cout<<"0000"; break;
             case '1': cout<<"0001";break;         
             case '2': cout<<"0010";break;       
             case '3': cout<<"0011";break;      
             case '4': cout<<"0100";break;    
             case '5': cout<<"0101";break;    
             case '6': cout<<"0110";break;     
             case '7': cout<<"0111";break;
             case '8': cout<<"1000";break;
             case '9': cout<<"1001";break;
            case 'A': cout<<"1010";break;       
             case 'B': cout<<"1011";break;      
             case 'C': cout<<"1100";break;    
             case 'D': cout<<"1101";break;    
             case 'E': cout<<"1110";break;     
             case 'F': cout<<"1111";break;         
             case 'a': cout<<"1010";break;       
             case 'b': cout<<"1011";break;      
             case 'c': cout<<"1100";break;    
             case 'd': cout<<"1101";break;    
             case 'e': cout<<"1110";break;     
             case 'f': cout<<"1111";break;
              default:  cout<<"This is notHexadecimal no"<<hexNumber[c];
         return 0;
        }  
    }
    cout<<endl;
}

Output
C++ Language  Program for Convert Number Hex to Binary

No comments:

Post a Comment