How can I achieve these conversions in C without using sprintf?
20 => 0x20
12 => 0x12
Currently I have:
int year = 12;
int month = 10;
int day = 9;
unsigned char date[3];
date[0] = year & 0xFF;
date[1] = month & 0xFF;
date[2] = day & 0xFF;
date will contain { 0x0C, 0x0A, 0x09 } but I want it to be { 0x12, 0x10, 0x09 }
For the limited 2-digit range you’re using:
etc.
You could express it as
((year / 10) << 4) | (year % 10)if that makes more sense to you.