BCD Calculator issue
I am converting a hex 0xE0
to BCD. When I do this I am getting back a 64. I know this is completely wrong and maybe it's something in my C++ code, but 64 just doesn't sound correct. Any ideas? Is 0xE开发者_StackOverflow0
a special case? (0xE0
is 224 in decimal.)
Here is part of my code:
unsigned char Hex2BCD(unsigned char param)
{ unsigned char lo;
unsigned char hi;
unsigned char val;
unsigned char buf[10];
hi = param/ 10;
lo = param- (hi * 10);
val= (hi << 4) + lo;
return val;
}
my idea is that your code for converting to BCD is buggy. it does not do what it is supposed to, thus the wrong result you are observing.
aside from this joke: 0xe0 if stored in signed char is a negative number. that could play nasty tricks on you if you don't pay special attention on the sign of temporary variables you are using while computing the result.
edit: now that you posted some code, it is clear that, although you compute the right value for the first digit into lo
, you need another step in order to get the right value into hi
.
using 0xe0
as input, you are actually computing (22<<4) + 4 = 356 = 0x164
instead of (2<<8)+(2<<4)+4 = 548 = 0x224
.
精彩评论