# Thread: Binary to Base 10 decimal conversion

1. ## 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. How about listing what you've already looked at so people wont suggest them again?

3. It doesn't take in binary decimals like 10101.001

No decimal places taken.

4. Are you talking about fixed point representation?

Floating point is a lot more common.

5. It wouldn't be that hard to write your own code to do it.

--
Mats

6. 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. 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

8. 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. 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

10. 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. Originally Posted by JFonseka
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

12. To do that, it would take me several hours lol.

13. 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. Sure, that'll work.

--
Mats