How can I convert a binary number in decimal ? I looked up how to do it but i can't find anything, I hope you could help.

Thanks.

How can I convert a binary number in decimal ? I looked up how to do it but i can't find anything, I hope you could help.

Thanks.

What do you mean? Often this kind of question is due to a misunderstanding, which is why you cannot find how to do it. Where is the binary number coming from? What form is it in? text? Where do you want the decimal form to appear?

Give us a snippet of code that shows how you would like to do it.

```
Serial.print (var, DEC) ;
```

Or do you mean something else?

Or if you actually looking for the conversion, then you would take each nibble from LSB to MSB and multiply it by 16 to the power of x.

0x1A2B3C -> (1 * pow(16, 5)) + (10 * pow(16, 4)) + (2 * pow(16,3)) + (11 * pow(16, 2)) + (3 * pow(16, 1)) + (12 * pow(16, 0)) = 171500410

Note: pow(16, x) = 16 ^ x (not XOR)

Or is this homework?

In the same vein as Hazards mind:

for a 4 bit number the values of each place is 8 4 2 1 If you have 1001 you add the value for each place that has a 1: 8 + 1 = 9

I hope the extension to 8, 32 and 64 bits is straightforward.

here is some code I have used in the past, it doesn’t matter the length of the binary number because it will increase the multiplier (b) each time it goes through the loop

```
long b, k, m, n;
long len, sum = 0;
len = strlen(binchar) - 1;
for(k = 0; k <= len; k++)
{
n = (binchar[k] - '0'); // char to numeric value
//Serial.print(n);
if ((n > 1) || (n < 0))
{
puts("\n\n ERROR! BINARY has only 1 and 0!\n");
}
for(b = 1, m = len; m > k; m--)
{
// 1 2 4 8 16 32 64 ... place-values, reversed here
b *= 2;
}
// sum it up
//Serial.println(sum);
sum = sum + n * b;
//Serial.println(sum); // uncomment to show the way this works
}
cardnum=sum;
```