Binary to Decimal Conversion

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Dec 16th, 2004, 3:47 am |
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.
Quick reply to this message  
C Syntax
  1. // Binary to Decimal, Hexadecimal and Octal conversion program
  2. // tested with Pelles C vegaseat 15dec2004
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int bin2dec(char *bin);
  8.  
  9. int main()
  10. {
  11. char bin[80] = "";
  12. char *p;
  13. int dec;
  14.  
  15. while(strcmp(bin,"0"))
  16. {
  17. printf("\n Enter a binary number (just 0 to EXIT): ");
  18. fgets(bin, sizeof(bin), stdin);
  19. // check for and remove trailing \n
  20. if ((p = strchr(bin,'\n')) != NULL)
  21. {
  22. *p = '\0';
  23. }
  24. dec = bin2dec(bin);
  25. if (dec) printf("\nDecimal = %d Hexadecimal = 0x%04X Octal = 0%o\n",dec,dec,dec);
  26. }
  27.  
  28. getchar(); // wait
  29. return 0;
  30. }
  31.  
  32. // convert a binary string to a decimal number, returns decimal value
  33. int bin2dec(char *bin)
  34. {
  35. int b, k, m, n;
  36. int len, sum = 0;
  37.  
  38. len = strlen(bin) - 1;
  39. for(k = 0; k <= len; k++)
  40. {
  41. n = (bin[k] - '0'); // char to numeric value
  42. if ((n > 1) || (n < 0))
  43. {
  44. puts("\n\n ERROR! BINARY has only 1 and 0!\n");
  45. return (0);
  46. }
  47. for(b = 1, m = len; m > k; m--)
  48. {
  49. // 1 2 4 8 16 32 64 ... place-values, reversed here
  50. b *= 2;
  51. }
  52. // sum it up
  53. sum = sum + n * b;
  54. //printf("%d*%d + ",n,b); // uncomment to show the way this works
  55. }
  56. return(sum);
  57. }
  58.  
0
Dave Sinkula Dave Sinkula is offline Offline | Dec 17th, 2004
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
 
0
prog-bman prog-bman is offline Offline | Dec 17th, 2004
Well you can read this why gets is bad
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
 
0
Dave Sinkula Dave Sinkula is offline Offline | Jan 5th, 2005
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.
 
0
agentmulder agentmulder is offline Offline | Apr 22nd, 2005
what does n = (bin[k] - '0') do?
 
0
vegaseat vegaseat is offline Offline | Apr 24th, 2005
like the comment says this turns for instance the character '1' to a numeric value 1
 
0
bumsfeld bumsfeld is offline Offline | Aug 1st, 2005
Wow, dude your code makes it very clear!
 
0
041020 041020 is offline Offline | Oct 7th, 2005
please make this program in c..e.g cout,cin
 
0
dilip.mathews dilip.mathews is offline Offline | Jun 27th, 2006
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);
}
Last edited by niek_e; 14 Days Ago at 2:34 pm. Reason: Added code-tags
 
0
vegaseat vegaseat is offline Offline | Jul 5th, 2006
A nice improvement by dilip.mathews! Thanks!
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC