Home All Groups Group Topic Archive Search About

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

Author
10 Oct 2005 3:51 PM
John Morley
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

Author
10 Oct 2005 4:05 PM
Dave
John

I think you should go and do some research into BCD.

The BCD for 10 is 2 bytes, one of 1 and one of 0.
In BCD is each decimal number expressed in binary so 1 to 12 is

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 0001 0000
11 0001 0001
12 0001 0010

Best Regards
Dave O.

Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
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
Author
10 Oct 2005 4:33 PM
John Morley
Hi,

Hmmm, perhaps I didn't ask the right question? The value "10" is made up
of two digits "1" and "0". "1" is represented as 0001, and "0" is 0000.
Put together, this becomes 00010000 which is decimal 16. I need a
routine to make this conversion (10 --> 16).

Thanks,

John



Dave wrote:
Show quoteHide quote
> John
>
> I think you should go and do some research into BCD.
>
> The BCD for 10 is 2 bytes, one of 1 and one of 0.
> In BCD is each decimal number expressed in binary so 1 to 12 is
>
> 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 0001 0000
> 11 0001 0001
> 12 0001 0010
>
> Best Regards
> Dave O.
>
> "John Morley" <jmorley@nospamanalysistech.com> wrote in message
> 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
>
>
>
Author
10 Oct 2005 4:39 PM
Veign
So you are looking for a routine that take a decimal number converts it to
BCD then reads the value as Binary and spits out the decimal equivalent?

Like:
Input: 25

Internal: 0010 0101

Output: 37

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
Veign's Blog
http://www.veign.com/blog
--


Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
news:OOb8VhbzFHA.1444@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> Hmmm, perhaps I didn't ask the right question? The value "10" is made up
> of two digits "1" and "0". "1" is represented as 0001, and "0" is 0000.
> Put together, this becomes 00010000 which is decimal 16. I need a routine
> to make this conversion (10 --> 16).
>
> Thanks,
>
> John
>
>
>
> Dave wrote:
>> John
>>
>> I think you should go and do some research into BCD.
>>
>> The BCD for 10 is 2 bytes, one of 1 and one of 0.
>> In BCD is each decimal number expressed in binary so 1 to 12 is
>>
>> 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 0001 0000
>> 11 0001 0001
>> 12 0001 0010
>>
>> Best Regards
>> Dave O.
>>
>> "John Morley" <jmorley@nospamanalysistech.com> wrote in message
>> 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
>>
>>
Author
10 Oct 2005 4:47 PM
Jim Mack
John Morley wrote:
> Hi,
>
> Hmmm, perhaps I didn't ask the right question? The value "10" is made
> up of two digits "1" and "0". "1" is represented as 0001, and "0" is
> 0000. Put together, this becomes 00010000 which is decimal 16. I need
> a routine to make this conversion (10 --> 16).
>

OK, what you're describing is 'packed BCD", which is a lot like hex, viewed as a binary byte.

Do you want a string that has the two characters "1" and "6", or just the value 16?

Try something like this:

Result = ((Dec \ 10) * 16) + (Dec Mod 10)

Res$ = Hex$(Result)

--

    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com
Author
10 Oct 2005 5:33 PM
Rick Rothstein [MVP - Visual Basic]
> > Hmmm, perhaps I didn't ask the right question? The value "10"
is made
> > up of two digits "1" and "0". "1" is represented as 0001, and
"0" is
> > 0000. Put together, this becomes 00010000 which is decimal 16.
I need
> > a routine to make this conversion (10 --> 16).
> >
>
> OK, what you're describing is 'packed BCD", which is a lot like
hex, viewed as a binary byte.
>
> Do you want a string that has the two characters "1" and "6", or
just the value 16?

That would be one of my questions. The other would be to ask what
the OP wants for numbers like 123 or 4567 or those containing more
digits.

Rick
Author
11 Oct 2005 10:16 AM
Dave
John

I suppose that if you look at it like that then "10" = "16" is sort of true,
I'm so used to binary and BCD that I make the conversion automatically and
would never look at it like that.

There is I'm sure proper ways to go about this but if you are only
interested in month values from 1 to 12 why not just add 6 to numbers above
9 thus:

if n > 9 then n = n + 6

that will give you 16,17 & 18 for 10, 11 & 12 repectively.

Regards
Dave.

Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
news:OOb8VhbzFHA.1444@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> Hmmm, perhaps I didn't ask the right question? The value "10" is made up
> of two digits "1" and "0". "1" is represented as 0001, and "0" is 0000.
> Put together, this becomes 00010000 which is decimal 16. I need a routine
> to make this conversion (10 --> 16).
>
> Thanks,
>
> John
>
>
>
> Dave wrote:
>> John
>>
>> I think you should go and do some research into BCD.
>>
>> The BCD for 10 is 2 bytes, one of 1 and one of 0.
>> In BCD is each decimal number expressed in binary so 1 to 12 is
>>
>> 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 0001 0000
>> 11 0001 0001
>> 12 0001 0010
>>
>> Best Regards
>> Dave O.
>>
>> "John Morley" <jmorley@nospamanalysistech.com> wrote in message
>> 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
>>
>>
Author
11 Oct 2005 10:32 AM
Mike Williams
"Dave" <nob***@nowhere.com> wrote in message
news:eVreBSbzFHA.3588@tk2msftngp13.phx.gbl...

> John, I think you should go and do some research into BCD.
> The BCD for 10 is 2 bytes, one of 1 and one of 0.

I think that's a little bit harsh, Dave. After all nobody (yet) is quite
sure what John really wants (mostly because his example makes it difficult
to come to any sensible deduction). Perhaps he'll post again and make his
requirements clear. You're of course right in saying that the BCD for
decimal 10 is two bytes - but that only applies to one kind of BCD. Another
kind of BCD (which was often used when I was "into" electronics many years
ago) was what is now called "packed BCD", in which each decimal number is
held in one "nibble" (there being, of course, two nibbles in every byte).
Back then most of us (at least most of the people I knew) used to simply
call it "BCD", rather than "packed BCD", simply because that is what we were
used to working with. However, in BCD the decimal number 10 would be held in
two bytes as (in hex representation to make things clear) &H010A and the
same decimal number 10 as "packed BCD" would be held in one byte as &H1A.

Personally I think it might be wise to wait un til the OP posts again so
that we know exactly what his requirements are.

Mike
Author
11 Oct 2005 11:56 AM
J French
On Tue, 11 Oct 2005 11:32:06 +0100, "Mike Williams"
<M***@WhiskyAndCoke.com> wrote:

<snip>

>However, in BCD the decimal number 10 would be held in
>two bytes as (in hex representation to make things clear) &H010A and the
>same decimal number 10 as "packed BCD" would be held in one byte as &H1A.

      0 to 9                     0 to 9
[128][64][32][16]  |   [8][4][2][1]
                      1         0  0  0  0

&h10   or  Chr$(16)  which ties up with the original post

>Personally I think it might be wise to wait un til the OP posts again so
>that we know exactly what his requirements are.

>Mike

I used to know a lot about packed BCD as that was the format for
Microsoft Business Basic, also for Track 2 on a credit card

'unpacked' BCD might as well be String Arithmetic
- something I've never seen in MS - but IIRC VAX BASIC supported it

Hmm...  I think the OP might be messing around with a device that
writes mag strips on credit cards.




Show quoteHide quote
>
Author
11 Oct 2005 12:28 PM
Mike Williams
"J French" <erew***@nowhere.uk> wrote in message
news:434ba469.273894686@news.btopenworld.com...

.. . . oops! I'm afraid I got that completely wrong! I knew exactly what I
wanted to write, but with Maureen hovering over my shoulder and wanting her
computer back it somehow didn't turn out that way :-(

Silly me.

Mike
Author
11 Oct 2005 12:37 PM
J French
On Tue, 11 Oct 2005 13:28:07 +0100, "Mike Williams"
<M***@WhiskyandCoke.com> wrote:

>"J French" <erew***@nowhere.uk> wrote in message
>news:434ba469.273894686@news.btopenworld.com...
>
>. . . oops! I'm afraid I got that completely wrong! I knew exactly what I
>wanted to write, but with Maureen hovering over my shoulder and wanting her
>computer back it somehow didn't turn out that way :-(

Not entirely wrong

- but better I caught it rather than leave it for posterity
Author
10 Oct 2005 4:16 PM
Jim Mack
John Morley wrote:
> 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).
>

I was with you until you got to "16".  I've never seen 10 decimal represented as 16 BCD.  Since what you want isn't 'standard' BCD, maybe you could give us some more examples of decimal numbers represented in the way you need them, and we could work out the relationship.


--

    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com
Author
10 Oct 2005 4:26 PM
Randy Birch
The BCD of 10 is  0001 0000, not 16.  Using your examples (10 >16) it
appears you are talking decimal>hex, ...

?&H10
16
?hex(16)
10

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/
----------------------------------------------------------------------------


Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
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
Author
10 Oct 2005 4:38 PM
John Morley
Randy,

I think you are right, although the controller manual I am using does
call the data "BCD"...... OK, so how do I programmatically convert the
10 to a 16? In your example, I see how you do it in the Immediate window
(?&H10), but how do I do that in code?

For example, I am trying to convert the month using:

iMonth = Month(mydate)

This returns "10" for October. I need to convert the "10" to a "16" to
pass to the controller.....

Thanks!

John

Randy Birch wrote:

Show quoteHide quote
> The BCD of 10 is  0001 0000, not 16.  Using your examples (10 >16) it
> appears you are talking decimal>hex, ...
>
> ?&H10
>  16
> ?hex(16)
> 10
>
Author
10 Oct 2005 8:02 PM
Randy Birch
Like this? ...

Private Sub Command1_Click()

   Dim nMonth As Variant
   Dim hexMonth As Variant
   Dim decMonth As Variant

   nMonth = Month(Now)
   Debug.Print nMonth, TypeName(nMonth)


   hexMonth = "&H" & nMonth
   Debug.Print hexMonth, TypeName(hexMonth)


   decMonth = CInt(hexMonth)
   Debug.Print decMonth, TypeName(decMonth)

End Sub


>  10           Integer
   &H10        String
    16           Integer


.... or as a function ...

Private Sub Command2_Click()

   Dim cnt As Integer
   For cnt = 1 To 12
      Print IntAsHex(cnt)
   Next

End Sub


Private Function IntAsHex(nInt As Integer) As Integer

   IntAsHex = CInt("&H" & nInt)

End Function


Seems to work for 1- through 19 but it's strange not using the A-F values in
hex.   For reference (comparison) see
http://www.htservices.com/Reference/NumberSystemConversions/

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/
----------------------------------------------------------------------------


Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
news:eLUCdkbzFHA.3812@TK2MSFTNGP09.phx.gbl...
: Randy,
:
: I think you are right, although the controller manual I am using does
: call the data "BCD"...... OK, so how do I programmatically convert the
: 10 to a 16? In your example, I see how you do it in the Immediate window
: (?&H10), but how do I do that in code?
:
: For example, I am trying to convert the month using:
:
: iMonth = Month(mydate)
:
: This returns "10" for October. I need to convert the "10" to a "16" to
: pass to the controller.....
:
: Thanks!
:
: John
:
: Randy Birch wrote:
:
: > The BCD of 10 is  0001 0000, not 16.  Using your examples (10 >16) it
: > appears you are talking decimal>hex, ...
: >
: > ?&H10
: >  16
: > ?hex(16)
: > 10
: >
Author
10 Oct 2005 8:09 PM
Duane Bozarth
Randy Birch wrote:
>
> Like this? ...
>
....snip sample code...

> Seems to work for 1- through 19 but it's strange not using the A-F values in
> hex.   For reference (comparison) see
> http://www.htservices.com/Reference/NumberSystemConversions/

I hesitated to jump in here as I (like apparently most others) am
totally confused by the description given by OP of what is really
wanted/needed by the hardware.

I'm thinking this sounds like documentation for some piece of hardware
written in what passes as an English translation--I've had a few
Advantech boards recently that were candidates for a contest of
poor/misleading/erroneous documentation. :(

Hopefully OP will tell us if one of the guesses so far happens to
actually solve his problem or can provide a more complete description of
the hardware and its descriptive usage if not...
Author
11 Oct 2005 11:17 PM
ck
"Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
news:434ACA86.D0AAC2A6@swko.dot.net...
> Randy Birch wrote:

>
> I'm thinking this sounds like documentation for some piece of hardware
> written in what passes as an English translation--I've had a few
> Advantech boards recently that were candidates for a contest of
> poor/misleading/erroneous documentation. :(
>

My favorite:

"Do not press the keys hardly...."

Chris A. Kusmierz
Author
11 Oct 2005 2:01 AM
DanS
John Morley <jmorley@nospamanalysistech.com> 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
Author
11 Oct 2005 6:09 AM
Mark Yudkin
Basically, you scan through the value dividing by 10, grab the remainder,
and accumulate on a nybble-basis. The problem with doing it in native VB6 is
that you have to do it yourself, rather than taking advantage of the Intel
hardware instruction for doing it.¨For this reason, I don't have any native
VB6 code, but instead call into a BCD package.

Show quoteHide quote
"John Morley" <jmorley@nospamanalysistech.com> wrote in message
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