Thread: Binary to Base 10 decimal conversion

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Binary to Base 10 decimal conversion

    Anyone know any good online converters?

    One that actually converts a binary decimal to base 10 decimal?

    Like 10101.001 -> Base 10 decimal.

    All converters I have found online round up the numbers.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How about listing what you've already looked at so people wont suggest them again?
    What about the calculator program that comes with your OS?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    It doesn't take in binary decimals like 10101.001

    No decimal places taken.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Are you talking about fixed point representation?

    Floating point is a lot more common.
    Last edited by mike_g; 11-20-2007 at 01:14 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It wouldn't be that hard to write your own code to do it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I would easily write my code, if I could find out the method for doing it properly, then checking it. Otherwise I would get wrong answers if my method was wrong

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The method is that you start with the same algorithm as for binary string to ascii [you could in fact do it with strtol() for this part] up to the decimal dot. After the dot, each digit is half of the previous, with the first one after the dot being "worth" 0.5.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well the main reason I was asking was because I have an exam tomorrow. And while I know how to do it, I was practicing some problems, but there were no answers, so I wanted to check my answers with some online converter.

    Writing a program would take a while :P

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
      for(;;) 
        {
          char input[100];
    
          char *p;
          double d = 0.0;
          double m = 0.5;
          int len;
          printf("Enter binary 'float' number (or 9 to exit):");
          if (fgets(input, sizeof(input), stdin) == NULL)
    	break;
          len = strlen(input);
          if (input[len-1]  == '\n') {
    	input[len-1] = 0;
          }
          else {
    	int ch;
    	printf("Error: input too long - ignored\n");
    	while((ch = getchar()) != '\n' && ch != EOF) ;
    	goto err;
          }
      
          p = input;
          if (*p == '9') {
    	break;
          }
          while(*p && *p != '.') {
    	if (*p < '0' || *p > '1') 
    	  {
    	    printf("Invalid input\n");
    	    goto err;
    	  }
    	d *= 2.0;
    	d += *p - '0';
    	p++;
          }
          if (*p) {
    	if (*p != '.') {
    	  printf("Expected a '.'\n");
    	  goto err;
    	}
    	p++;
    	while(*p) {
    	  if (*p < '0' || *p > '1') 
    	    {
    	      printf("Invalid input\n");
    	      goto err;
    	    }
    	  d += (*p - '0') * m;
    	  m *= 0.5;
    	  p++;
    	}
          }
          printf("Decimal: &#37;f\n", d);
        err:
          continue;
        }
      return 0;
    }
    How about that one - didn't take too long, did it?

    Edit: Appologies for short variable names and use of goto. If anyone would like to take the code and modify it to use longer names and no goto's, feel free.



    --
    Mats
    Last edited by matsp; 11-20-2007 at 05:45 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Pretty cool mats, I guess I'll use it =) but I won't modify it because it will take me a while to understand it lol.

    Thanks, did you actually type all that out?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JFonseka View Post
    Pretty cool mats, I guess I'll use it =) but I won't modify it because it will take me a while to understand it lol.

    Thanks, did you actually type all that out?
    Sure, it's only about 50 or so lines. Not that much - and a bit of copy'n'paste.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    To do that, it would take me several hours lol.

  13. #13
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Sorry for bringing up a somewhat "old" topic, but you could also have done this, ie:

    Code:
        10101.001
    =
        10101001 * 2^-3
    =       <convert to decimal, let's say with XP calculator>
        169 * 2^-3
    =
        169 / 2^3
    =
        169 / 8
    =
        21.125
    Tadam, by using only XP calculator (in scientific mode).

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, that'll work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM
  4. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM
  5. binary to decimal
    By jk81 in forum C Programming
    Replies: 1
    Last Post: 09-13-2002, 05:20 AM