Decimal, Hex, octal and binary number inter conversion

shabbir's Avatar author of Decimal, Hex, octal and binary number inter conversion
This is an article on Decimal, Hex, octal and binary number inter conversion in C.
Rated 5.00 By 1 users
The article discusses about all the number formats viz Binary, Decimal, Octal, Hex and BCD (Binary coded decimal) and conversion from Decimal to Binary, Octal and Hex and also the reverse conversion.

Binary



A numbering system based on 2 in which 0 and 1 are the only available digits.

Decimal



decimal fraction: a proper fraction whose denominator is a power of 10

Octal



A numbering system that uses eight digits, 0 through 7. It is used as a shorthand system for representing binary characters that use six bits.

Hexa Decimal



A numbering system which uses a base of 16. The first ten digits are 0-9 and the next six are A-F.

Binary to Decimal


Code: C
void Bin2Dec()
{
    int bin,n,r,s=0,i;
    printf("Enter a binary number\n");
    scanf("%d",&bin);
    n=bin;
    for(i=0;n!=0;i++)
    {
        r=n%10;
        s=s+r*(int)pow(2,i);
        n=n/10;
    }
    printf("The equivalent number of %d is %d\n",bin,s);
}

Octal to Decimal


Code: C
void Oct2Dec()
{
    int oct,n,r,s=0,i;
    printf("Enter an octal number\n");
    scanf("%d",&oct);
    n=oct;
    for(i=0;n!=0;i++)
    {
        r=n%10;
        s=s+r*(int)pow(8,i);
        n=n/10;
    }
    printf("The equivalent number of %d is %d\n",oct,s);
}

Hex to Decimal


Code: C
void Hex2Dec()
{
    char hex[N];
    int i,j,n[N],l;
    long double dec=0;
    printf("Enter the hexa decimal number and find it's decimal equivalent\n");
    fflush(stdin);
    gets(hex);
    l=strlen(hex);
    for(i=0;i<l;i++)
    {
        switch(hex[i])
        {
        case '0':
            n[i]=hex[i]-48;     //Ascii code of 0 is 48  48-48=0//
            break;
        case '1':
            n[i]=hex[i]-48;  //Ascii code of 1 is 49  49-48=1//
            break;
        case '2':
            n[i]=hex[i]-48;  //Ascii code of 2 is 50  50-48=2//
            break;
        case '3':
            n[i]=hex[i]-48;  //Ascii code of 3 is 51  51-48=3//
            break;
        case '4':
            n[i]=hex[i]-48;  //Ascii code of 4 is 52  52-48=4//
            break;
        case '5':
            n[i]=hex[i]-48;  //Ascii code of 5 is 53  53-48=5//
            break;
        case '6':
            n[i]=hex[i]-48;  //Ascii code of 6 is 54  54-48=6//
            break;
        case '7':
            n[i]=hex[i]-48;  //Ascii code of 7 is 55  55-48=7//
            break;
        case '8':
            n[i]=hex[i]-48;  //Ascii code of 8 is 56  56-48=8//
            break;
        case '9':
            n[i]=hex[i]-48;  //Ascii code of 9 is 57  57-48=9//
            break;
        case 'A':
            n[i]=hex[i]-55;     //Ascii code of A is 65 65-55=10//
            break;
        case 'B':
            n[i]=hex[i]-55;  //Ascii code of B is 65 66-55=11//
            break;
        case 'C':
            n[i]=hex[i]-55;  //Ascii code of C is 65 67-55=12//
            break;
        case 'D':
            n[i]=hex[i]-55;  //Ascii code of D is 65 68-55=13//
            break;
        case 'E':
            n[i]=hex[i]-55;  //Ascii code of E is 65 68-55=14//
            break;
        case 'F':
            n[i]=hex[i]-55;  //Ascii code of F is 65 69-55=15//
            break;
        }
    }
    for(i=0,j=l;i<l;i++,j--)
        dec=dec+(n[j-1]*pow(16,i));
    printf("The decimal equivalent is %lg \n",dec);
}

Decimal to Binary


Code: C
void Dec2Bin()
{
    int n,bin[100],i,j;
    printf("Enter A Number To Find It's Binary Equivalence\n");
    scanf("%d",&n);
    printf("The Binary Equivalent of %d is \t",n);
    for(i=0;n!=0;i++)
    {
        bin[i]=n%2;
        n=n/2;
    }
    for(j=i-1;j>=0;j--)
    {
        printf("%d",bin[j]);
    }
    printf("\n");
}

Decimal to Octal


Code: C
void Dec2Oct()
{
    int n,r[10],i;
    printf("Enter a number to find it's octal equivalent\n");
    scanf("%d",&n);
    printf("The octal equivalent of %d is ",n);
    for(i=0;n!=0;i++)
    {
        r[i]=n%8;
        n=n/8;
    }
    i--;
    for(;i>=0;i--)
        printf("%d",r[i]);
    printf("\n");
}

Decimal to Hex


Code: C
void Dec2Hex()
{
    int n,r[10],i;
    printf("Enter a number to get its hexadecimal equivalent\n");
    scanf("%d",&n);
    for(i=0;n!=0;i++)
    {
        r[i]=n%16;
        n=n/16;
    }
    i--;
    for(;i>=0;i--)
    {
        if(r[i]==10)
            printf("A");
        else if(r[i]==11)
            printf("B");
        else if(r[i]==12)
            printf("C");
        else if(r[i]==13)
            printf("D");
        else if(r[i]==14)
            printf("E");
        else if(r[i]==15)
            printf("F");
        else
            printf("%d",r[i]);
    }
    printf("\n");
}

Binary Coded Decimal (BCD)



A coding system in which each decimal digit from 0 to 9 is represented by a 4-digit binary number.
Attached Files
File Type: zip NumberConv.zip (1.2 KB, 412 views)
ehsano like this
0
christina's Avatar, Join Date: Nov 2006
Newbie Member
hi mr. shabbir. i saw your program conversion of decimal nos to binary which is our assignment, here in go4experts.com

uhm...if you dont mind...will please put comments so i will be able to fully understand the program.thanks!
Code:
void Dec2Bin()
{
    int n,bin[100],i,j;
    printf("Enter A Number To Find It's Binary Equivalence\n");
    scanf("%d",&n);
    printf("The Binary Equivalent of %d is \t",n);
    for(i=0;n!=0;i++)
    {
        bin[i]=n%2;
        n=n/2;
    }
    for(j=i-1;j>=0;j--)
    {
        printf("%d",bin[j]);
    }
    printf("\n");
}
thank you very much.i will be waiting for your reply.

Last edited by shabbir; 16Nov2006 at 08:26.. Reason: Code formating.
0
christina's Avatar, Join Date: Nov 2006
Newbie Member
if anyone can help me.pls someone put comments in mr. shabbir's program.thank you....
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Where you want me to place the comment I dont understand
0
ayat_Ace's Avatar, Join Date: Jan 2007
Newbie Member
how can i write these programs with assemly languge 8088
thank you
0
ayat_Ace's Avatar, Join Date: Jan 2007
Newbie Member
who can help me
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by ayat_Ace
who can help me
Its just you who can help yourself. Apart from that if you are stuck somewhere we can.
0
ayat_Ace's Avatar, Join Date: Jan 2007
Newbie Member
thank you mr shabir any way but can help me where i can find the answer so that i can quickl reach it !
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by ayat_Ace
thank you mr shabir any way but can help me where i can find the answer so that i can quickl reach it !
What answers you are talking about.
0
ayat_Ace's Avatar, Join Date: Jan 2007
Newbie Member
hi mr shabbir
the answers i mean that you seems to me a very skillful experts so advise me to be good at assemly programming i mean that you know more than me about very powerful websites help me to improve my self in programming

thanks alot
could you send me your email please
my email : ayat_ace@yahoo.com