This isn't a regular "binary to bcd" question, in fact, I'm not really sure what to call the thing I'm trying to do!
There is a single byte in an embedded device that stores the numbers 1 through 7 (for days of the week) in the following format:
00000001 = 1
00000010 = 2
00000100 = 3
00001000 = 4
00010000 = 5
00100000 = 6
01000000 = 7
I want to read this byte, and convert its contents (1 through 7) into BCD, but I'm not sure how to do this.
I know I could just brute-force it with a series of if statements:
if(byte == B00000001)
{
answer = 1;
}
else
if(byte == B00000010)
{
answer = 2;
}
and so on, but I think there could be a better way. This data is stored in a single register on a real time clock. I'm getting this byte by performing an I2C read, and I read it into a byte in my program. The datasheet for this real-time clock specifies that this particular register is formatted as I have outlined above.