Friday, 20 June 2014

C++ program to convert hexadecimal to binary number

Here is a C++ program to convert hexadecimal to binary. Here are the different methods to convert hexadecimal to binary like using recursion, using bitwise operator etc.
How to convert octal to binary (base 16 to base 2).
To convert hexadecimal to binary a simple just convert each digit of hexadecimal number to binary .For example let us convert hexadecimal number (D5)16 to binary.
D = (1101)2
5 = (0101)2
(D5)16 = (11010101)2.


Method 1 : C++ program to convert hexadecimal to binary
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
    long i,hexa[20],rem,binary[4],z=1;
    char input[20];
    cout<<"Enter the number in Hexadecimal ";
    gets(input);
    cout<<"Binary equivalent : ";
    for(i=0;input[i]!='\0';i++)
    {
        if(input[i]>='A')
            hexa[i]=input[i]-55;
        else if(input[i]>='a')
            hexa[i]=input[i]-87;
        else
            hexa[i]=input[i]-48;
        for(int y=0;y<4;y++)
        {
            binary[y]=hexa[i]%2;
            hexa[i]/=2;
        }
        for(int y=3;y>=0;y--)
            cout<<binary[y];
    }
    return 0;
}



Method 2 : C++ program to convert hexadecimal to binary using functions
#include<iostream>
#include<math.h>
using namespace std;
void bin(char *input)
{
    int hexa[20],binary[4];
    cout<<"Binary equivalent = ";
    for(int i=0;input[i]!='\0';i++)
    {
        if(input[i]>='A')
            hexa[i]=input[i]-55;
        else if(input[i]>='a')
            hexa[i]=input[i]-87;
        else
            hexa[i]=input[i]-48;
        for(int y=0;y<4;y++)
        {
            binary[y]=hexa[i]%2;
            hexa[i]/=2;
        }
        for(int y=3;y>=0;y--)
            cout<<binary[y];
    }
}
int main()
{
    char inp[20];
    cout<<"Enter the number in hexadecimal ";
    cin>>inp;
    bin(inp);
    return 0;
}




Method 3 : C++ program to convert hexadecimal to binary using recursion
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
void binary(int n,int z)
{
    if(z==4)
        return ;
    binary(n/2,z+1);
    cout<<n%2;
}
void hex(char *str,int i)
{
    if(str[i]=='\0')
        return ;
    if(str[i]>=65&&str[i]<=70)
         binary(str[i]-55,0);
    else if(str[i]>=97)
        binary(str[i]-87,0);
    else
        binary(str[i]-48,0);
    hex(str,i+1);
}
int main()
{
    char str[10];
    cout<<"Enter Hexadecimal number : ";
    cin>>str;
    cout<<"Binary equivalent : ";
    hex(str,0);
    return 0;
}



Method 4 : C++ program to convert hexadecimal to binary using bitwise operator
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
    long i,hexa[20],amask,k;
    char input[20];
    cout<<"Enter the number in Hexadecimal ";
    gets(input);
    cout<<"Binary equivalent : ";
    for(i=0;input[i]!='\0';i++)
    {
        if(input[i]>='A')
            hexa[i]=input[i]-55;
        else if(input[i]>='a')
            hexa[i]=input[i]-87;
        else
            hexa[i]=input[i]-48;
        for(int y=3;y>=0;y--)
        {
           amask=1<<y;
           k=hexa[i]&amask;
           k==0?cout<<"0":cout<<"1";
        }
    }
    return 0;
}





Method 5 : C++ program to convert hexadecimal to binary and store it as string
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
    long i,hexa[20],z=0;
    char input[20],res[80];
    cout<<"Enter the number in Hexadecimal ";
    gets(input);
    cout<<"Binary equivalent : ";
    for(i=strlen(input)-1;i>=0;i--)
    {
        if(input[i]>='A')
            hexa[i]=input[i]-55;
        else if(input[i]>='a')
            hexa[i]=input[i]-87;
        else
            hexa[i]=input[i]-48;
        for(int y=0;y<4;y++)
        {
            res[z++]=hexa[i]%2+48;
            hexa[i]/=2;
        }
        res[z]='\0';
    }
    strrev(res);
    cout<<res;
    return 0;
}

No comments:

Post a Comment