# Covertion of decimal to binary, octal, and hexadecimal numbers

Page 1 of 1

## 7 Replies - 22000 Views - Last Post: 29 August 2007 - 10:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=18812&amp;s=ff9f74882cd2dd047fbb06d07197518d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 apoli87

Reputation: 1
• Posts: 7
• Joined: 20-September 06

# Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 21 September 2006 - 02:59 AM

```#include<stdio.h>
#include<conio.h>
#include<string.h>

void bd();
void db();
void doc();
void dh();
void od();
void ob();
void bo();
void bh();
void hb();
void hd();
void oh();
void ho();

void main()
{
int n;
char c;
begin:
clrscr();
printf("1.  Binary to Decimal.\n");
printf("2.  Decimal to Binary.\n");
printf("3.  Decimal to Octal.\n");
printf("5.  Octal to Decimal.\n");
printf("6.  Octal to Binary.\n");
printf("7.  Binary to Octal.\n");
scanf("%d",&n);
if(n<1 || n>12)
printf("Invalid Choise\n");
if(n==1)
bd();
else if(n==2)
db();
else if(n==3)
doc();
else if(n==4)
{
long a;
clrscr();
printf("Enter the decimal number.\n");
scanf("%ld",&a);
dh(a);
}
else if(n==5)
od();
else if(n==6)
ob();
else if(n==7)
bo();
else if(n==8)
bh();
else if(n==9)
hb();
else if(n==10)
hd();
else if(n==11)
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("Enter a Octal number\n");
scanf("%ld",&n);
i=0;
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==0)
{
oh(i);
}
else if(t==1)
printf("Numbert entered is not octal.\n");
}
else if(n==12)
ho();
printf("\nDo you Wish to continue(Y/N)\n");
scanf("%s",&c);
if(c=='Y')
goto begin;
getch();
}

void bd()
{
int n,b=0,a[6],i=0,t=0;
clrscr();
printf("Conversion from Binary to Decimal\n");
printf("Enter Binary Number\n");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]!=1 && a[i]!=0)
t=1;
i++;
}
a[i]=2;
n=1;
for(i=0;a[i]!=2;i++)
{
b=b+a[i]*n;
n=n*2;
}
if(t==0)
printf("Decimal Equivalent=%d",B);
else if(t==1)
printf("Entered number is not binary.");

}

void db()
{
int dec,bin,n,i=0,a[10];
clrscr();
printf("Conversion from Decimal to Binary\n");
printf("Input decimal no.");
scanf("%d",&dec);
do
{
a[i]=dec%2;
dec=dec/2;
i++;
}while(dec!=0);
for(n=i-1;n>=0;n--)
printf("%d",a[n]);
}

void doc()
{
int n,i,a[10];
clrscr();
printf("Conversion from Decimal to Octal\n");
printf("Enter a Decimal number\n");
scanf("%d",&n);
i=0;
printf("Octal equavalent of %d is ",n);
while(n!=0)
{
a[i]=n%8;
n=n/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}

void dh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
dh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}

void od()
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("Conversion from Octal to Decimal\n");
printf("Enter a Octal number\n");
scanf("%ld",&n);
i=0;
printf("Decimal equavalent of %ld",n);
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==0)
printf("= %ld",i);
else if(t==1)
printf(" can't be calculated because it is not an Octal Number.\n");
}

void ob()
{
int n,a[6],i=0,t=0;
clrscr();
printf("Convertion from Octal to Binary.\n");
printf("Enter an Octal Number.\n");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]>7)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("000");
break;
case 1:
printf("001");
break;
case 2:
printf("010");
break;
case 3:
printf("011");
break;
case 4:
printf("100");
break;
case 5:
printf("101");
break;
case 6:
printf("110");
break;
case 7:
printf("111");
break;
}
}
if(t==1)
printf("Not a Octal number\n");
}

void bo()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Convertion From Binary to Octal\n");
printf("Enter a Binary number\n");
scanf("%ld",&n);
while(n!=0)
{
a[i]=n%1000;
n=n/1000;
if(a[i]>111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
default:
printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
break;
}
}
if(t==1)
printf("Number is not Binary\n");
}

void bh()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Enter a Binary number\n");
scanf("%ld",&n);
while(n!=0)
{
a[i]=n%10000;
n=n/10000;
if(a[i]>1111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
case 1000:
printf("8");
break;
case 1001:
printf("9");
break;
case 1010:
printf("A");
break;
case 1011:
printf("B");
break;
case 1100:
printf("C");
break;
case 1101:
printf("D");
break;
case 1110:
printf("E");
break;
case 1111:
printf("F");
break;
default:
printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
break;
}
}
if(t==1)
printf("Number is not Binary\n");
}

void hb()
{
int i;
char s[20];
clrscr();
scanf("%s",s);
//gets(s);
printf("Binary Equivalent=");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'a':
case 'A':
printf("1010");
break;
case 'b':
case 'B':
printf("1011");
break;
case 'c':
case 'C':
printf("1100");
break;
case 'd':
case 'D':
printf("1101");
break;
case 'e':
case 'E':
printf("1110");
break;
case 'f':
case 'F':
printf("1111");
break;
default:
printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
break;
}
}
}

void hd()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
scanf("%s",s);
printf("Decimal Equivalent=");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
printf("%ld ",h);
}

void oh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
oh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}

void ho()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
scanf("%s",s);
// Converting hex to dec first
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
// Now convering from decimal to octal (h)
i=0;
printf("Octal equavalent=");
while(h!=0)
{
a[i]=h%8;
h=h/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}

```

Is This A Good Question/Topic? 1

## Replies To: Covertion of decimal to binary, octal, and hexadecimal numbers

### #2 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 21 September 2006 - 03:44 AM

Why are you posting such a CRAP?
See the compilation result.

Quote

D:\MinGW\bin>gcc -Wall -pedantic test.c
test.c:19: warning: return type of 'main' is not `int'
test.c: In function `main':
test.c:23: warning: implicit declaration of function `clrscr'
test.c:74: warning: long int format, long unsigned int arg (arg 2)
test.c:97: error: stray '\169' in program
test.c:97: error: `toupper' undeclared (first use in this function
test.c:97: error: (Each undeclared identifier is reported only onc
test.c:97: error: for each function it appears in.)
test.c: In function `bd':
test.c:126: error: `cool' undeclared (first use in this function)
test.c:126: error: syntax error before ';' token
test.c: In function `db':
test.c:134: warning: unused variable `bin'
test.c: In function `od':
test.c:212: warning: long int format, long unsigned int arg (arg 2
test.c:423:1: warning: C++ style comments are not allowed in ISO C
test.c:423:1: warning: (this will be reported only once per input
test.c: In function `hb':
test.c:425: warning: comparison between pointer and integer
test.c: In function `hd':
test.c:500: warning: comparison between pointer and integer
test.c: In function `ho':
test.c:619: warning: comparison between pointer and integer
test.c:700:2: warning: no newline at end of file

This post has been edited by Xing: 21 September 2006 - 03:45 AM

### #3 apoli87

Reputation: 1
• Posts: 7
• Joined: 20-September 06

## Re: Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 21 September 2006 - 03:49 AM

Try to make a program that will input decimal value and will convert it in binary, octal and hexadecimal.

------------------------------------------------

**Example of the output of the program**

Enter the decimal value: 10

Binary value is: 1010

Octal value is: 12

### #4 Java_N1ZJA

Reputation: 0
• Posts: 48
• Joined: 25-August 06

## Re: Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 21 September 2006 - 05:27 AM

Hi

Thanks

### #5 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 184
• Posts: 4,673
• Joined: 26-November 04

## Re: Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 21 September 2006 - 07:03 AM

If you're intention is to contribute your code, post it in the Code Snippets Section. (With corrected errors obviously).

What do you want to do with the code?
Add Code Tags around source code to reduce the display size of the output post.

### #6 Jayman

• Student of Life

Reputation: 420
• Posts: 9,532
• Joined: 26-December 05

## Re: Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 21 September 2006 - 07:58 AM

Same topic posted in 2 different forums by same member.

Topics have been merged into one. PM me if these are actually not related and I will put them back.

### #7 arem026

Reputation: 1
• Posts: 22
• Joined: 28-August 07

## Re: Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 29 August 2007 - 09:52 PM

does anyone here can give me code of converting binary to decimal only

### #8 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: Covertion of decimal to binary, octal, and hexadecimal numbers

Posted 29 August 2007 - 10:47 PM

arem026, on 30 Aug, 2007 - 10:22 AM, said:

does anyone here can give me code of converting binary to decimal only

Search the snippets section.