Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/19/03 22:43
Read: times


 
#56903 - RE: converting 5 bit bdc to hex
Responding to: ???'s previous message
Converting BCD is very simple, once you get past the initial confusion. Normally, a single nibble (4 bits) stores a number between 0 and 15 in decimal, or 0 to F in hexadecimal. In order to make it binary coded decimal, you simply disallow any number greater than 9 (1001 binary) in a single nibble. Notice that until you exceed 9 (1001) in a given nibble there is no difference between the decimal number and the hexadecimal number. It's no more complicated than that.
You need to determine if you are using true BCD data or "packed" BCD data. With true BCD, the MSN (most significant nibble) of each byte will be 0000. If this is not the case, then you are using packed BCD. In true BCD, every decimal digit is represented by a binary byte of the same value. a 1 digit number uses 1 byte. a two digit number uses two bytes, 3 digits - 3 bytes, etc.

True BCD:

dec BCD
0 0000 0000
1 0000 0001
2 0000 0010
3 0000 0011
4 0000 0100
5 0000 0101
6 0000 0110
7 0000 0111
8 0000 1000
9 0000 1001
10 0000 0001 0000 0000
11 0000 0001 0000 0001
.
.
.
231 0000 0010 0000 0011 0000 0001
.
.
.
etc...

Packed BCD works exactly the same, except you don't waste the MSN. In packed BCD format each byte represents two decimal digits. It is still true that no nibble can contain a value greater than 9 (1001). Pcaked BCD would then look like this.

Packed BCD:

00 0000 0000
01 0000 0001
02 0000 0010
03 0000 0011
04 0000 0100
05 0000 0101
06 0000 0110
07 0000 0111
08 0000 1000
09 0000 1001
10 0001 0000
11 0001 0001
12 0001 0010
.
.
.
31 0011 0001
32 0011 0010
.
.
.
0231 0000 0010 0011 0001
.
.
.
etc...

So you see, you can NOT simply treat BCD as hexidecimal in the general case. Consider the case of 231 in packed BCD (seen above). If you simply treat it as Hex, you will get 0x0231 = 0d561. And it's worse if you have true BCD ( 0x020301 = 0d131841).
Moreover, it is rather misleading to talk about a 5-bit BCD value. The data can exist in only four of those bits; the fifth bit is superfluous. I'm wondering if you might be dealing with a five DIGIT BCD number (which would be 5 bytes long, or two bytes and a nibble in packed BCD).
Now, if the one digit is truely the entire number, then you can simply store it and call it hex since there is no difference. However, if the digit is just one digit of a larger number, you must convert it carefully. Let me know if you have any more questions, or if I've only made matters worse, and I'll try to be of more and better help.

List of 14 messages in thread
TopicAuthorDate
converting 5 bit bdc to hex            01/01/70 00:00      
   RE: converting 5 bit bdc to hex            01/01/70 00:00      
      RE: converting 5 bit bdc to hex            01/01/70 00:00      
   RE: converting 5 bit bdc to hex            01/01/70 00:00      
      RE: converting 5 bit bdc to hex            01/01/70 00:00      
         RE: converting 5 bit bdc to hex            01/01/70 00:00      
         No such thing as 5-bit BCD!            01/01/70 00:00      
   RE: not clear            01/01/70 00:00      
      RE: not clear            01/01/70 00:00      
         RE: not clear            01/01/70 00:00      
         RE: not clear - a valuable lesson            01/01/70 00:00      
         RE: not clear            01/01/70 00:00      
   RE: converting 5 bit bdc to hex            01/01/70 00:00      
      RE: converting 5 bit bdc to hex            01/01/70 00:00      

Back to Subject List