Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Decimal, Hex, octal and binary number inter conversion (http://www.go4expert.com/articles/decimal-hex-octal-binary-inter-t1548/)

shabbir 5Oct2006 21:04

Decimal, Hex, octal and binary number inter conversion
 
1 Attachment(s)
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.

christina 15Nov2006 21:31

Re: Decimal, Hex, octal and binary number inter conversion
 
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. :)

christina 15Nov2006 21:32

Re: Decimal, Hex, octal and binary number inter conversion
 
if anyone can help me.pls someone put comments in mr. shabbir's program.thank you....

shabbir 16Nov2006 08:26

Re: Decimal, Hex, octal and binary number inter conversion
 
Where you want me to place the comment I dont understand

ayat_Ace 6Jan2007 14:40

Re: Decimal, Hex, octal and binary number inter conversion
 
how can i write these programs with assemly languge 8088
thank you

ayat_Ace 6Jan2007 14:46

Re: Decimal, Hex, octal and binary number inter conversion
 
who can help me

shabbir 6Jan2007 15:50

Re: Decimal, Hex, octal and binary number inter conversion
 
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.

ayat_Ace 8Jan2007 16:18

Re: Decimal, Hex, octal and binary number inter conversion
 
thank you mr shabir any way but can help me where i can find the answer so that i can quickl reach it ! :)

shabbir 8Jan2007 17:03

Re: Decimal, Hex, octal and binary number inter conversion
 
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.

ayat_Ace 9Jan2007 17:33

Re: Decimal, Hex, octal and binary number inter conversion
 
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

shabbir 9Jan2007 17:40

Re: Decimal, Hex, octal and binary number inter conversion
 
Quote:

Originally Posted by ayat_Ace
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

My email add is in the forum. Click on the contact us or also on my profile.

rai_gandalf 22Feb2007 02:55

Re: Decimal, Hex, octal and binary number inter conversion
 
Hey Shabbir,
Very useful thread, this one. The thing is one becomes so proficient in programming & concerned with advanced implementation issues, that one often forgets small snippets & nuggets of code such as this. ;) So, Thx a lot for it. :)

Waise, I developed a Decimal Integer to Binary String or Hexa String Convertor. I have put the source code here .... ;)

Ciao,
Rajiv


Code:

/* Decimal Integer to Binary String or Hexa String Convertor */

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#define DEFAULTVAL 0

char* dec_to_binstr(int,int=DEFAULTVAL);
char* dec_to_hexstr(int,int=DEFAULTVAL);

void main()
  {
  int choice;
  unsigned int decqty,numbits;

  do{
    clrscr();
    printf("1. DEC TO BIN\t2. DEC TO HEX\n3. EXIT");
    printf("\nEnter choice :  ");
    scanf("%d",&choice);

    if(choice==1 || choice==2)
      {
      printf("\n\n\nEnter the Decimal Quantity :  ");
      scanf("%d",&decqty);
      printf("\nEnter a specific Number of Bits/Hexa-Digits if you want the O/P to contain a specific number of bits");
      printf("\n(Enter '0' (or DEFAULTVAL) to leave blank so that program calculates required number of bits) :  ");
      scanf("%d",&numbits);
      }

    switch(choice)
      {
      default:
        printf("\n\nINVALID CHOICE!! Please Try Again!!");
      case 1:
        printf("\n\nBinary Equivalent is :  %s",dec_to_binstr(decqty,numbits));
        break;
      case 2:
        printf("\n\nHexadecimal Equivalent is :  %s",dec_to_hexstr(decqty,numbits));
        break;
      case 3:
        break;
      }
    getch();
    }while(choice!=3);
  }

/* This Routine Converts a decimal Qty to binary Qty */
char* dec_to_binstr(int decnum,int numbits)
  {
  int i,j,k;
  char *result;
  char *temp;

  printf("\n\n\nEntered in Routine :  dec_to_binstr()");
  printf("\ndecnum = %d\tnumbits = %d",decnum,numbits);

  // Calculating Number of bits required
  for(i=0;pow(2,i)<abs(decnum);i++);

  printf("\nNo. of bits reqd (i) = %d",i);

  // Checking Whether 'numbits' provided in Call is adequate
  if(numbits!=DEFAULTVAL && numbits<i)
    return "ERROR!! Incorrect 'numbits' Specification during Call!!";

  // Allocating Memory for 'temp' & 'result'
  else if(numbits==DEFAULTVAL)
    // No 'numbits' provided in call
    {
    temp=(char*)malloc((i+1)*sizeof(char));
    result=(char*)malloc((i+1)*sizeof(char));
    }
  else
    // Function call has provided the 'numbits'
    // Assuming that 'numbits' involves the 1 extra character slot required
    // in case of signed integers
    {
    temp=(char*)malloc((numbits+1)*sizeof(char));
    result=(char*)malloc((numbits+1)*sizeof(char));
    }

  // Performing Intermediate processing on 'temp'
  for(j=0;decnum>0;j++)
    {
    temp[j]=char('0'+decnum%2);
    decnum=decnum/2;
    }

  // Padding Any Bits Remaining if at all with Zeroes
  for(;j<numbits;j++)
    temp[j]='0';
  temp[j]='\0';

  // Forming the 'result' string by String Reversal
  k=0;

  if(numbits==DEFAULTVAL)
    j=i-1;
  else
    j=numbits-1;

  for(;j>=0;j--,k++)
    result[k]=temp[j];
  result[k]='\0';

  return result;
  }

char* dec_to_hexstr(int decnum,int numbits)
  {
  int i,j,k;
  char *result;
  char *temp;
  int numtemp;
 
  printf("\n\n\nEntered in Routine :  dec_to_hexstr()!!");
  printf("\ndecnum = %d\tnumbits = %d",decnum,numbits);

  // Calculating Number of Hexa-digits required
  for(i=0;pow(16,i)<abs(decnum);i++);

  printf("\nNo. of Hexa-digits reqd (i) = %d",i);

  // Checking Whether 'numbits' provided in Call is adequate
  if(numbits!=DEFAULTVAL && numbits<i)
    return "ERROR!! Incorrect 'numbits' Specification during Call!!";

  // Allocating Memory for 'temp' & 'result'
  else if(numbits==DEFAULTVAL)
    // No 'numbits' provided in call
    {
    temp=(char*)malloc((i+1)*sizeof(char));
    result=(char*)malloc((i+1)*sizeof(char));
    }
  else
    // Function call has provided the 'numbits'
    // Assuming that 'numbits' involves the 1 extra character slot required
    // in case of signed integers
    {
    temp=(char*)malloc((numbits+1)*sizeof(char));
    result=(char*)malloc((numbits+1)*sizeof(char));
    }

  // Performing Intermediate processing on 'temp'
  for(j=0;decnum>0;j++)
    {
    numtemp=decnum%16;
    if(numtemp>=10)
      temp[j]=char('A'+(numtemp-10));
    else
      temp[j]=char('0'+numtemp);
    decnum=decnum/16;
    }

  // Padding Any Bits Remaining if at all with Zeroes
  for(;j<numbits;j++)
    temp[j]='0';
  temp[j]='\0';

  // Forming the 'result' string by String Reversal
  k=0;

  if(numbits==DEFAULTVAL)
    j=i-1;
  else
    j=numbits-1;

  for(;j>=0;j--,k++)
    result[k]=temp[j];
  result[k]='\0';

  return result;
  }


shabbir 22Feb2007 08:51

Re: Decimal, Hex, octal and binary number inter conversion
 
Quote:

Originally Posted by rai_gandalf
Very useful thread, this one. The thing is one becomes so proficient in programming & concerned with advanced implementation issues, that one often forgets small snippets & nuggets of code such as this. ;) So, Thx a lot for it. :)

My pleasure.

Quote:

Originally Posted by rai_gandalf
Waise, I developed a Decimal Integer to Binary String or Hexa String Convertor. I have put the source code here .... ;)

Thats a good prog and when I will reach home will try running it.

christina 26Mar2007 18:34

Re: Decimal, Hex, octal and binary number inter conversion
 
hi mr shabbir. i hope you receive my email to you. thank you very much to your program. i ask my other proffessor in school to help me, and he did, at first i dont understand but later on when i spend time (much time n_n' ) on i finally did it. all i can say that somehow, ur a genius sir.hee. coz i dont have the talent you possess. my talent is in drawing, i could be a future flash animator or a web developer.hee.ooopppss!!! why am i saying this.hee.sorry.
But let me give you my WARM-BIG THANKS SIR!!!
my prof gave me a grade of 91 for my effort and hardwork in hios subject.c=
thanks a LOT!!

shabbir 26Mar2007 18:51

Re: Decimal, Hex, octal and binary number inter conversion
 
christina, the pleasure is all mine but I would suggest one thing. Try to give something to the programming community from where you have got something and that may not necessarily be this forum.

kunalkunal 1Apr2007 17:19

Need Help!
 
Hey Shabbir,
Can you help me something?
I am BCA 1st sem. Student; I want a program in C language.

Write a program to transform its input according to a specified transformation scheme. The transformation scheme will consist of two strings: a string of characters and then a string of replacement characters. The idea is that your program replaces every instance of the ith character in the initial string with the (i+3) character (of English alphabets) in the replacement string. When no substitution is defined for a character, the program just passes it through to the output unchanged. The program should inform the user of any errors in the transformation scheme. Your program should display the phrase before and after the substitutions have been made.
Example:
Original String: This is a C program.
String after the transformation: Wklv lv d F Surjudp.


Pls help
Thanks in advance

shabbir 1Apr2007 18:00

Re: Decimal, Hex, octal and binary number inter conversion
 
kunalkunal, 2 things I would suggest to get the best out of any forums / groups.

1. Dont jump into others topic / article with your query.
2. Dont ask us to complete your assignments but you can always ask us the best way of doing it.

kunalkunal 1Apr2007 18:05

Re: Decimal, Hex, octal and binary number inter conversion
 
you are right but i am new in c programming, then how can i done it ?
so pls give me some tips
thanks

shabbir 1Apr2007 18:25

Re: Decimal, Hex, octal and binary number inter conversion
 
Just create a thread with your query in the Queries and discussion section

gbagnati 11May2007 23:45

Re: Decimal, Hex, octal and binary number inter conversion
 
One question to understand the conversion program from Decimal to Binary.
How does the variable bin[100] work?

masoudxxx2002 9Nov2010 17:04

Re: Decimal, Hex, octal and binary number inter conversion
 
Thank you very much:crazy:

ehsano 24Nov2010 22:40

Re: Decimal, Hex, octal and binary number inter conversion
 
thank you mr.shabir you very good:nice::nice::nice:

shabbir 25Nov2010 08:41

Re: Decimal, Hex, octal and binary number inter conversion
 
Quote:

Originally Posted by ehsano (Post 75188)
thank you mr.shabir you very good:nice::nice::nice:

The pleasure is all mine

seshu 27Dec2010 11:18

Re: Decimal, Hex, octal and binary number inter conversion
 
Hii,,
i need the code for md5 in c language...
plz help me

gatsbycollege 28Dec2011 11:36

Re: Decimal, Hex, octal and binary number inter conversion
 
hi mr shabbir, got a question here,, can you increase the maximum input of convertion, if i input 32767+ i get wrong convertion thx for the reply

shabbir 28Dec2011 15:57

Re: Decimal, Hex, octal and binary number inter conversion
 
Quote:

Originally Posted by gatsbycollege (Post 90626)
hi mr shabbir, got a question here,, can you increase the maximum input of convertion, if i input 32767+ i get wrong convertion thx for the reply

Why don't you try the same and share it as an article here. I am sure my code is good enough to explain you the concept.

gatsbycollege 28Dec2011 16:16

Re: Decimal, Hex, octal and binary number inter conversion
 
Code:

#include <stdio.h>
#include <conio.h>
main ()
{
    int n,r[10],i;
    clrscr();
    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");
    getch();
    return 0;
}

i tried to change some part because all i need is the convertion,, but im getting a problem in trying to input a decimal value which is 32767+ then convert to hex,

shabbir 28Dec2011 16:25

Re: Decimal, Hex, octal and binary number inter conversion
 
May be because that is the limit of int and see if changing the data type helps.

gatsbycollege 28Dec2011 16:33

Re: Decimal, Hex, octal and binary number inter conversion
 
Quote:

Originally Posted by shabbir (Post 90648)
May be because that is the limit of int and see if changing the data type helps.

yes, tried that thing earlier,,it point me out on using double data type, but the problem is that the code use "%" ,, so i tried fmod,, still no luck,


All times are GMT +5.5. The time now is 13:22.