简体   繁体   English

BCD计算器问题

[英]BCD Calculator issue

I am converting a hex 0xE0 to BCD. 我正在将十六进制0xE0转换为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. 当我这样做时,我得到的是64。我知道这是完全错误的,也许这是我的C ++代码中的问题,但是64听起来不正确。 Any ideas? 有任何想法吗? Is 0xE0 a special case? 0xE0是特例吗? ( 0xE0 is 224 in decimal.) 0xE0为十进制224。)

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. 我的想法是,您转换为BCD的代码存在错误。 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. 除了这个笑话:如果存储在带符号的char中,则为0xe0为负数。 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 . 编辑:现在,您已经发布了一些代码,很显然,尽管您将第一个数字的正确值计算为lo ,但是还需要执行另一步骤才能将正确的值获取为hi

using 0xe0 as input, you are actually computing (22<<4) + 4 = 356 = 0x164 instead of (2<<8)+(2<<4)+4 = 548 = 0x224 . 使用0xe0作为输入,您实际上是在计算(22<<4) + 4 = 356 = 0x164而不是(2<<8)+(2<<4)+4 = 548 = 0x224

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM