Re: Converting decimal to BCD (binary coded decimal)...
- From: DanS <t.h.i.s.n.t.h.a.t@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 10 Oct 2005 21:01:06 -0500
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
.
- References:
- Converting decimal to BCD (binary coded decimal)...
- From: John Morley
- Converting decimal to BCD (binary coded decimal)...
- Prev by Date: Re: Avoid Overflow!
- Next by Date: Re: Grabbing Email
- Previous by thread: Re: Converting decimal to BCD (binary coded decimal)...
- Next by thread: Raised To The Power Of!
- Index(es):
Relevant Pages
|