Some compilers may have built in functions for these convertions, but others don't. Below are two functions for converting a BCD number to decimal and vice versa:
And a brief calling example:
I am waiting feedback for better or more effective converting ways.
Cheers!
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 #define uint8 unsigned char uint8 BCDToDecimal (uint8 bcdByte) { return (((bcdByte & 0xF0) >> 4) * 10) + (bcdByte & 0x0F); } uint8 DecimalToBCD (uint8 decimalByte) { return (((decimalByte / 10) << 4) | (decimalByte % 10)); }
And a brief calling example:
Code C - [expand] 1 2 3 4 5 uint8 decNum = 52; uint8 bcdNum = 0x39; uint8 convertedToBCD = DecimalToBCD(decNum); //convertedToBCD = 0x52 uint8 convertedToDec = BCDToDecimal(bcdNum); //convertedToDec = 39
I am waiting feedback for better or more effective converting ways.
Cheers!