Re: Converting decimal to BCD (binary coded decimal)...



John Morley <jmorley@xxxxxxxxxxxxxxxxxxxxxx> wrote in news:ugxYLKbzFHA.3000
@TK2MSFTNGP12.phx.gbl:

> Hi All,
>
> Is there a handy way to convert a decimal value to it's equivalent BCD
> representation in VB?? I need to send some data to a controller that
> expects date data in BCD. For example the month of October (decimal 10)
> would be sent as "16" (BCD equivalent of 10).
>
> Thanks!
>
> John
>

Hi John,

I didn't even know that BCD was used like this, as I've only used it in
rotary switches. I'm not exactly sure why this would be used. Sending "10"
as a string would be 2 bytes, and BCD would be 1, but just straight binary
would be one also.

So the others know, here's an article:

http://www.danbbs.dk/~erikoest/bcd.htm

Specifically, your talking about packed-BCD. Each digit is represented by 4
bits of a byte.

As per your example, October, month 10, BCD=16.....

0000 0000 = 1 Byte
0001 = 1
0000 = 0

00010000 = 16 in BCD

I got this to work with some long code that had 3 functions, but while
looking at the output, and noticed a pattern.

Since one byte represents 2 digits, the following works on a 2 digit number
up to 99. That may be an easy limitation to work with.

CodeBegin
-----------------------------------------------------
Public Function BCDxx(decNum As Integer) As Integer

BCDxx = Int(decNum / 10) * 16
BCDxx = BCDxx + (decNum Mod 10)

End Function
-----------------------------------------------------
CodeEnd

That's what the pattern worked out to be, I can post the long code if
needed.

But, simple is almost always better.

Regards,

DanS
.



Relevant Pages

  • Re: in which form real and integer values are saved in memory
    ... >> twos complement, BCD or others. ... BP> I don't think that BCD is a valid representation for C integer ... essentially the entire program state at every point). ...
    (comp.lang.c)
  • Re: What about big integers in Ada 2005?
    ... > BCD was originally a six bit representation. ... > the advent of the byte on the IBM System 360, BCD became Extended Binary ... The 6-bit character code was BCDIC -- Binary Coded Decimal Interchange ... > in the picture and a nibble for the sign. ...
    (comp.lang.ada)
  • Re: in which form real and integer values are saved in memory
    ... >> intergers may be big endian or little endian, ... >> twos complement, BCD or others. ... BP> I don't think that BCD is a valid representation for C integer ...
    (comp.lang.c)
  • Re: understanding floats
    ... > exercise) to write floating point math routines that represent numbers ... > an analogous problem for a base 10 representation when dealing with ... and represented BCD numbers in 80 bits. ... I also seem to recall the ...
    (comp.lang.lisp)
  • Re: Currency Rounding Errors
    ... BCD offers no significant advantages over fixed-point binary. ... representation of 1234 was, in fact, the characters "1234". ... own data type and writing your own operations on that data type. ... "decimal fixed-point and floating-point are still important and continue ...
    (microsoft.public.vc.mfc)