# Binary to Decimal Conversion

The computer is a binary beast. We want to go from the one-fingered digital box to the ten-fingered human being and convert binary to denary(base 10) numbers. This is how it works, for instance, binary 10010 is calculated as 1*16+0*8+0*4+1*2+0*1 = decimal 18. Just because it's easy, let's throw in hexadecimal and octal results too. The added bonus is the almost foolproof entry of data.

2,130 Views

Scientist

``````// Binary to Decimal, Hexadecimal and Octal conversion program
// tested with Pelles C     vegaseat     15dec2004

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

int bin2dec(char *bin);

int main()
{
char bin = "";
char *p;
int  dec;

while(strcmp(bin,"0"))
{
printf("\n Enter a binary number (just 0 to EXIT): ");
fgets(bin, sizeof(bin), stdin);
// check for and remove trailing \n
if ((p = strchr(bin,'\n')) != NULL)
{
*p = '\0';
}
dec = bin2dec(bin);
if (dec) printf("\nDecimal = %d  Hexadecimal = 0x%04X  Octal = 0%o\n",dec,dec,dec);
}

getchar();  // wait
return 0;
}

// convert a binary string to a decimal number, returns decimal value
int bin2dec(char *bin)
{
int  b, k, m, n;
int  len, sum = 0;

len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
n = (bin[k] - '0'); // char to numeric value
if ((n > 1) || (n < 0))
{
puts("\n\n ERROR! BINARY has only 1 and 0!\n");
return (0);
}
for(b = 1, m = len; m > k; m--)
{
// 1 2 4 8 16 32 64 ... place-values, reversed here
b *= 2;
}
// sum it up
sum = sum + n * b;
//printf("%d*%d + ",n,b);  // uncomment to show the way this works
}
return(sum);
}
``````

test.c 11 error [Warning 603] Symbol 'bin' (line 8) not initialized
test.c 14 error [Warning 534] Ignoring return value of function 'gets(char *)'
test.c 14 error [Warning 421] Caution -- function 'gets(char *)' is considered dangerous
test.c 17 error [Info 725] Expected positive indentation from line 16
test.c 19 error [Warning 534] Ignoring return value of function '_fgetc(struct {...} *)'
test.c 29 error [Info 713] Loss of precision (assignment) (unsigned int to int)
test.c 47 error [Info 818] Pointer parameter 'bin' (line 24) could be declared as pointing to const

It was a linter, by the way.

scanf("%s",bin);

This is just a diguised version of gets(). It suffers the same ills.

". Written by some real winner named HAMMER."

...who apparently still knows more than you.

what does n = (bin[k] - '0') do?

like the comment says this turns for instance the character '1' to a numeric value 1

Wow, dude your code makes it very clear!

please make this program in c..e.g cout,cin

A slight modification for the bin2dec function. Removes the nested for loop. Reduces the overhead and removes the temporary variable 'm'.

``````int bin2dec(char *bin)
{
int  b , k, n;
int  len, sum = 0;

len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
b = 1;
n = (bin[k] - '0'); // char to numeric value
if ((n > 1) || (n < 0))
{
puts("\n\n ERROR! BINARY has only 1 and 0!\n");
return (0);
}
b = b<<(len-k);
// sum it up
sum = sum + n * b;
//printf("%d*%d + ",n,b);  // uncomment to show the way this works
}
return(sum);
}``````

A nice improvement by dilip.mathews! Thanks!

Nice program!!!

i'm favorly new to C and was wondering how the
line b<<(len-k) worked... all i know its shifts the digits to the left..
but what i dont get is by how much..

....ie. bin<<1 =multiply it by 2 so what does len-k do in the above statement..

hope that doesnt' sound confusing..

and also what does teh sum stand for in "sum=sum+n*b"

cheers

your code is much long.. i found one in net
try it

``````long bin2dec(char *s)
{
long r=0;
for (; *s; r = (r<<1) | (*s++ - '0'));
return r;
}``````

Good for you, but shorter is not always better. There is no error trapping, or explanation/comment how it works!

binary 10010 is calculated as 1*16+0*8+0*4+1*2+0*1 = decimal 18
hence b = b<<(len-k) does the multiplication by 1,2,4,8,16 ...

sum = sum + n * b;
// this will explain it ...
printf("%d*%d + ",n,b);

``````/*conversion of binary number to decimal*/
#include <stdio.h>

int main()
{
int binary, digit, base = 0, decimal = 0;
printf("enter a binary number: ");
scanf("%d", &binary);
printf("Entered binary number is: %d\n", binary);
while(binary)
{
digit = binary % 10;
decimal += digit << base;
base += 1;
binary /= 10;
}
printf("decimal equivalent of the entered binary number is: %d", decimal);
return 0;
}``````

//decimal to binary

``````#include<stdio.h>
void main()

{

long int deci,rem,bin,p=1;
printf("enter no");
scanf("%ld",&deci);
if (deci > 0)

{
while (deci > 0)

{
rem=deci%2;
bin=rem*p+bin;
p=p*10;
deci=deci%2;

}
printf("binary no is %ld",bin);
//end of while loop

getchar();

}

//end of condition

else

printf("binary no for +tive integer only");

getchar();

}``````

Nice code everyone! It's funny - I just had to do the same thing for a class the other day in school.

I uploaded the code to my blog... here it is: <URL SNIPPED>

Also here:

``````int bin2dec(char * binstr)
{
int dec = 0;
int len = strlen(binstr);
int i = 0;

while(i < len)
{
dec = (dec << 1) + (binstr[i] - 0x30);
i++;
}
return dec;
}``````

That's my bin2dec function. I wrote a full explanation about it on my site (or at least tried :P) Good to compare with the others.

guys what will hapeen if we enter 101.11001

``````#include<conio.h>
#include<stdio.h>
#include<math.h>
{
float n,c1,m1,ans;
int a,b,r,i=0,m,s=0,s1=0,r2,j=-1;
clrscr();
printf("enter the binary no:");
scanf("%f",&n);
b=n;
a=n%1;
while(b>0)
{
r=b%10;
c=pow(2,i);
m=r*c;
s=s+m;
b=b/10;
i++;
}
if(a>0)
{
while(a>0)
{
r1=a%10;
s1=s1*10+r1;
a=a/10;
}
while(s1>0)
{
r2=s1%10;
c1=pow(2,j);
m1=r2*c1;
s2=s2+m1;
s1=s1/10;
j--;
}}
ans=s+s2;
printf("the decimal is %f",ans);
getch();
}
``````
WaltP commented: What a piece ot crap! No formatting, conio.h, no comments. Terrible!! -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.