04-16-2003, 05:46 AM
Is there some class or code to convert BCD numbers to ASCII (and viceversa) in VB6? I get BCD numbers to buffer and I need to show ASCII (decimal) numbers.
Can somebody to help me, please?
04-16-2003, 10:27 PM
BCD is Binary coded decimal and is nothing more than crippled Hex. In
Hex you can take a byte and break it into two nibbles (4 bits) then
convert the value of each of those nibbles into a single hex digit. BCD is
the same thing except values of each nibble only go up to 9 and the a-f
hex values are skipped. This means one byte can represent all the
values from 0 to 99 (100 values). So just convert each byte to hex, then
use Val or CInt or CLng to convert to a number.
Ex. The largest BCD value in a byte is 153 (99). Val(Hex(153)) = 99
04-16-2003, 11:27 PM
Just to expand a little on what Thinker said. There are also asm instructions that will deal with BCD, such as: AAA, AAS, AAD, DAA, DAS etc. However, using VB, you're probably not interested in them. :p
If you're going to be doing this on a large scale, its more efficient to use numbers rather than strings. To get the upper/lower nibbles from a byte you can also do this:
153 and &hf0 \ 16 ' mask off the high nibble and shift right 4 bits
153 and &hf ' mask off the low nibble
04-17-2003, 03:56 AM
Thanks OnErrOr for your help and thanks to Thinker in Machaira's name.
I can't believe it how simple solution is.