Binary to Decimal

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Dave Sinkula Dave Sinkula is offline Offline Jan 6th, 2005, 3:57 pm |
0
This is a very common task for a beginner programmer. Unfortunately many beginners do not seem to understand the question.

First, know that binary, decimal, octal, hexadecimal, and others are simply representations of values. The value of 123 (decimal) still has the same value if it represented in hexadecimal (7B), octal (173), or binary (1111011). So the first thing to know is that you are not converting a binary value to a decimal value.

What is the question then? You are likely being asked to convert the text representation of a value from binary notation into a value. So it is a conversion of text to an integral value.

Like each digit in 123 has place value (1 is in the hundreds place, 2 is in the tens place, and 3 is in the units place), each bit in a binary number has place value as well. Starting from the least significant bit, each next significant bit has twice the place value.

Okay, here's some code.
Quick reply to this message  
C Syntax
  1. #include <stdio.h>
  2.  
  3. /* Convert the text representation of a binary number into an integral value. */
  4. unsigned int btou(const char *bit)
  5. {
  6. unsigned int value = 0;
  7. while ( *bit != NULL ) /* loop through all of the text representation */
  8. {
  9. value *= 2; /* double the previous result */
  10. value += *bit - '0'; /* add current bit (0 or 1 from '0' or '1') */
  11. ++bit; /* go on to the next bit */
  12. }
  13. return value;
  14. }
  15.  
  16. int main ( void )
  17. {
  18. static const char *binary[] =
  19. {
  20. "0000","0001","0010","0011","0100","0101","0110","0111",
  21. "1000","1001","1010","1011","1100","1101","1110","1111",
  22. "1010010110100101", "11011110101011011011111011101111",
  23. };
  24. size_t i;
  25. for ( i = 0; i < sizeof binary / sizeof *binary; ++i )
  26. {
  27. unsigned int value = btou(binary[i]);
  28. printf("btou(\"%s\") = 0x%X = %u\n", binary[i], value, value);
  29. }
  30. return 0;
  31. }
  32.  
  33. /* my output
  34. btou("0000") = 0x0 = 0
  35. btou("0001") = 0x1 = 1
  36. btou("0010") = 0x2 = 2
  37. btou("0011") = 0x3 = 3
  38. btou("0100") = 0x4 = 4
  39. btou("0101") = 0x5 = 5
  40. btou("0110") = 0x6 = 6
  41. btou("0111") = 0x7 = 7
  42. btou("1000") = 0x8 = 8
  43. btou("1001") = 0x9 = 9
  44. btou("1010") = 0xA = 10
  45. btou("1011") = 0xB = 11
  46. btou("1100") = 0xC = 12
  47. btou("1101") = 0xD = 13
  48. btou("1110") = 0xE = 14
  49. btou("1111") = 0xF = 15
  50. btou("1010010110100101") = 0xA5A5 = 42405
  51. btou("11011110101011011011111011101111") = 0xDEADBEEF = 3735928559
  52. */
  53.  
  54. #if 0
  55. /* of course, if you can use standard library functions, it would be simpler yet */
  56. #include <stdlib.h>
  57. unsigned int btou(const char *bit)
  58. {
  59. return strtoul(bit, NULL, 2);
  60. }
  61. #endif
0
Dave Sinkula Dave Sinkula is offline Offline | Jan 9th, 2005
Converting from Fahrenheit to Celcius would be converting from one representation to another. The keyword here for those who need to practice reading for comprehension, is 'representation' -- not value. I take it you are commenting on my description and not the code. Apparently you have not yet seen enough of the newb question, 'How can I convert integers to binary or hexadecimal?', which has its own FAQ. Knowing the difference between text and an integer becomes important and germaine to the issue, whether the direction is this way or that.
 
0
bumsfeld bumsfeld is offline Offline | Sep 10th, 2005
Dave, you are correct here, there is difference between representation and conversion.
 
0
Kellaw Kellaw is offline Offline | Mar 5th, 2006
how do you do it using stack?
 
0
al3x748769 al3x748769 is offline Offline | Jan 13th, 2009
how would u turn it into an 8 bit counter
 
 

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC