0

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 0xE0 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;
}
Questioneer
  • 761
  • 4
  • 12
  • 20
  • 6
    You want us to help you with your code, without showing us the code? – Alok Save Oct 07 '11 at 15:58
  • Forget i even mentioned code.... Anyone know a good hex to BCD convertor? Quit down rating my post.. – Questioneer Oct 07 '11 at 16:01
  • 1
    0xE0 is 224 decimal, so the result should be 0x224. no need for a BCD converter to compute that result. – Adrien Plisson Oct 07 '11 at 16:03
  • 1
    It's a poorly constructed question, but it is a real question. It did not deserve closing for that reason. – JeremyP Oct 07 '11 at 16:07
  • 1
    @JeremyP: **Without the source code, It was not a real Q** Period! Now that the OP has posted source code, It becomes a valid one, So i will vote to reopen. – Alok Save Oct 07 '11 at 16:12
  • 1
    What's your question exactly? – Platinum Azure Oct 07 '11 at 16:28
  • 2
    @Questioneer: think about how many bits to you need to store 224 in BCD format, and how many bits you have at your disposal in the `Byte` type that you return. – Mat Oct 07 '11 at 16:34
  • 1
    @Als: I disagree, it was a very bad question until the source code was posted, but it needed fixing, not closing. Down voting and a comment would have been a more appropriate action. – JeremyP Oct 10 '11 at 07:09

1 Answers1

1

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.

Adrien Plisson
  • 22,486
  • 6
  • 42
  • 73
  • if i were using all unsigned types would this still be the case? – Questioneer Oct 07 '11 at 16:04
  • 1
    i don't know, you did not show us any code to comment on. we don't have a magical eight-ball telling us where the errors are in code we are only told about. – Adrien Plisson Oct 07 '11 at 16:06
  • what is the fix to this? thanks for ur help what i mean is can you give me the syntax in order to do this – Questioneer Oct 07 '11 at 16:30
  • Also, was just noticing that 0x224 in ur response. I'm asking about E0 in hex and it being 224 in "decimal" – Questioneer Oct 07 '11 at 17:15
  • 1
    do you know what bcd is ? your first step should have been [documenting yourself !](http://en.wikipedia.org/wiki/Binary-coded_decimal) once you understand what bcd is, you will understand why i am talking about 0x224. now, there is enough hints in my answer for you to correct your mistake. (and no, i won't do your own work !) – Adrien Plisson Oct 07 '11 at 17:35
  • val = (hi << 8) + (hi << 4) + 4; gives me the same value that I was getting. – Questioneer Oct 07 '11 at 17:42
  • 1
    let's take another example: `bcd(0x210) = bcd(528) = (5<<8)+(2<<4)+8 = 0x528 = 1320`... – Adrien Plisson Oct 07 '11 at 20:26