Richard A. Cini wrote:
I'm working on a real-time-clock
emulation for the Altair32 and
I need a push on something. Most RTC boards return a BCD value to the
caller. When you call GetLocalTime in Windows, it returns an integer value.
So, I have to somehow convert an integer to its BCD representation.
I thought about using sprintf on the integer and then returning
str[0] or str[1] depending on if I need the tens place or units place.
However, this seems to me to be an inelegant way of doing it.
Does anyone have any ready-made C code to do this that I can
use? Thanks a lot!
You want packed BCD, right?
Horrible kludge but for something quick:
for (i=0; i<16?; i+=4) {
digit = value % 10;
value /= 10;
digit <<= i;
result |= digit;
}
There are much more elegant algorithms that you can write in
assembly language (but I've been told NO ONE uses assembly
language anymore so I won't bother you with those :> )