bcdToDec

I can't get this to work ! If I use 10 binary I get 10 Not 2.....

byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
void setup() {
Serial.begin(9600);
Serial.println(bcdToDec(10));
}
void loop() { }

Can SomeOne Help?
END.

Why would that surprise you?

BCD means Binary Coded Decimal, is different from binary representation:

Let me save you some time.

10bcd is an invalid number. Being BCD, 9dec = 9bcd = 9hex. 10dec = 0x10bcd (16bcd) = 0x0ahex.

OP, Did you mean Serial.println(bcdToDec(0b10));?

dhenry:
10bcd is an invalid number. Being BCD, 9dec = 9bcd = 9hex. 10dec = 0x10bcd (16bcd) = 0x0ahex.

Over the years I have (successfully) used BCD representation a number of times and was confident I fully understood it until I attempted to read that.

Fortunately a quick perusal of the (English) Wiki article brought it all back to me.

Shane99875:
I can't get this to work ! If I use 10 binary I get 10 Not 2.....

I don't know what you meant "10 binary" to mean but "binary" is almost certainly not the right word here.

It looks as if your code is trying to split the incoming byte into two nibbles and convert each nibble into a decimal digit, and combine the two digits into a value (which would be 0 .. 99). It looks to me as if the code would do that correctly, although you'd need to test it to make sure.

The problem I suspect is that you aren't using the correct BCD input value to test your code. If the answer is intended to be the decimal number 10, the input BCD byte would need to contain the two nibbles {1, 0} which would be BCD encoded as 0x10 (decimal 16).