Friday 20 June 2014

C++ program to convert binary to Hexadecimal number


Here is a C++ program to convert binary to hexadecimal. Here are the different methods to convert binary to hexadecimal like using recursion.
How to convert binary to hexadecimal (base 2 to base 16).
To convert binary to hexadecimal just take four bits and convert it in decimal number .For example let us convert binary number (10101011)2 to hexadecimal. Note if the number is greater than 9 we write alphabets A,B,C,D,E,F in place of 10,11,12,13,14,15.
(1011)2 decimal equivalent is 11 so write B
(1010)2 decimal equivalent is 10 so write A.
Thus equivalent of hexadecimal number is (AB)16.


Method 1 : C++ program to convert binary to hexadecimal
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int i,temp=0,hexa[20],rem,num;
    cout<<"Enter the number in binary ";
    cin>>num;
    cout<<"Hexadecimal eqivalent ";
    for(i=0;num>0;i++)
    {
        for(int y=0;y<4;y++)
        {
             rem=num%10;
             num/=10;
             temp+=rem*pow(2,y);
       }
       hexa[i]=temp;
       temp=0;
    }
    while(i>0)
    {
        i--;
        if(hexa[i]>9)
            cout<<char(55+hexa[i]);
        else
            cout<<hexa[i];
    }
    return 0;
}




Method 2 : C++ program to convert binary to hexadecimal using recursion
#include<iostream>
#include<math.h>
using namespace std;
int deci(int n,int i)
{
    if(n==0)
        return 0;
    return (n%10)*pow(2,i)+deci(n/10,i+1);
}
void hex(int n)
{
    int hexa;
    if(n==0)
        return;
    hex(n/10000);
    hexa=deci(n%10000,0);
    if(hexa>9)
        cout<<char(55+hexa);
    else
        cout<<hexa;
}
int main()
{
    int num;
    cout<<"Enter Binary number : ";
    cin>>num;
    cout<<"Hexadecimal equivalent : ";
    hex(num);
    return 0;
}



Method 3 : C++ program to convert binary to hexadecimal using function
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
void hex(int num)
{
    int i,temp=0,hexa[20],rem;
    cout<<"Hexadecimal eqivalent ";
    for(i=0;num>0;i++)
    {
        for(int y=0;y<4;y++)
        {
             rem=num%10;
             num/=10;
             temp+=rem*pow(2,y);
       }
       hexa[i]=temp;
       temp=0;
    }
    while(i>0)
    {
        i--;
        if(hexa[i]>9)
            cout<<char(55+hexa[i]);
        else
            cout<<hexa[i];
    }
}
int main()
{
    int num;
    cout<<"Enter the number in binary ";
    cin>>num;
    hex(num);
    return 0;
}


Method 4 : C++ program to convert binary to hexadecimal and store it as string
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
    char bin[40],hex[10];
    int i=0,z=0,y=0,temp=0;
    cout<<"Enter the number in binary ";
    cin>>bin;
    z=strlen(bin)-1;
    while(z>=0)
    {
        for(y=0;y<4&&z>=0;y++)
             temp+=(bin[z--]-48)*pow(2,y);
        if(temp>9)
            hex[i++]=temp+55;
        else
            hex[i++]=temp+48;
        temp=0;
    }
    hex[i]='\0';
    strrev(hex);
    cout<<"Hexadecimal equivalent "<<hex;
    return 0;
}


No comments:

Post a Comment