1,105,534 Community Members

Binary to Decimal Conversion

Member Avatar
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
 
0
 

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.

// 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[80] = "";
  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);
}
Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

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

Member Avatar
prog-bman
Junior Poster
108 posts since Nov 2004
Reputation Points: 4 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

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.

Member Avatar
agentmulder
Newbie Poster
1 post since Apr 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

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

Member Avatar
bumsfeld
Posting Virtuoso
1,537 posts since Jul 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 261 [?]
Skill Endorsements: 7 [?]
 
0
 

Wow, dude your code makes it very clear!

Member Avatar
041020
Newbie Poster
1 post since Oct 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 3 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
}
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

A nice improvement by dilip.mathews! Thanks!

Member Avatar
lester pinto
Newbie Poster
1 post since Aug 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
declum
Newbie Poster
4 posts since Feb 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
 }
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

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);

Member Avatar
CSOL
Newbie Poster
1 post since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
/*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;
}
Member Avatar
krishna5687
Newbie Poster
1 post since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

//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();


				}
Member Avatar
taylorg
Newbie Poster
3 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

soorajshaji
Newbie Poster
2 posts since Jul 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

guys what will hapeen if we enter 101.11001

soorajshaji
Newbie Poster
2 posts since Jul 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
-1
 
#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();
}
You
Post:
Start New Discussion
View similar articles that have also been tagged: