Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BCD toDecimal conversion 1

Status
Not open for further replies.

loubee

Technical User
Mar 3, 2002
4
GB
I need to convert a BCD number to a decimal number.
I have been able to convert it by sub devision but this is a lenghty process and not very efficient.
Is there a simple conversion factor? I have tried to work one out but without success.

Regards

Lou B.
 
Can you elaborate a bit more? and Is the BCD packed?

When dealing with this sort of thing your best to use bitshifting on it to extract what you need. Bit shifting is much faster than arithmetic operations. William
Software Engineer
ICQ No. 56047340
 
William,

Does VB provide bit shift functions? Can you list some?

Thanks

Jason
 
AND XOR OR Not only are they logical operators in VB they are also bitwise as well.

William
Software Engineer
ICQ No. 56047340
 
williamu (Programmer) Apr 13, 2002
Can you elaborate a bit more? and Is the BCD packed?

Thanks for your quick response.

Is the BCD packed? Bear with me I'm a novice. Can I give you an example. 10032 (BCD) results in 1273 (Decimal).Does that answer your question?

I am reading this from an address (in Fltsim 2002) and the result should give me a readout for a frequency display.

Freq 112.73 = 10032(BCD) the first 1 of 112 is a dummy.


Regards

Lou
 
BCD is simply hexadecimal where the individual characters
are limited to between 0 and 9 rather than 0 to F.
So to convert from a decimal BCD number, convert it to
Hex and then back to decimal i.e:-

Private Sub Command1_Click()
Dim Lng_Val As Long
Dim Str_BCD As String

Str_BCD = Hex(10032)
Lng_Val = Val(Str_BCD)
'Put something here to get rid of the dummy

End Sub
 

SJA (TechnicalUser) Apr 15, 2002
BCD is simply hexadecimal where the individual characters
are limited to between 0 and 9 rather than 0 to F.
So to convert from a decimal BCD number, convert it to
Hex and then back to decimal
*********************************************************
SJA
Thanks, it's so easy when you know how. But difficult if you don't <bg>

Sincere thanks

Lou B




 
Hi All,
Maybe it's just because of Tax Shock, but I have to confess being totally confused by this thread. I've worked with BCD numbers for over 20 years, and thought I &quot;got it&quot;.

First, let's start with the decimal number 1273.


In an ASCII string, it would be represented internally as &quot;1273&quot;:
3 1 3 2 3 7 3 3
0011 0010 0011 0010 0011 0111 0011 0011


In binary digits, it would be represented internally as 495:
4 9 5
0100 1001 0101
(1 x 1024 + 1 x 128 + 1 x 16 + 1 x 4 + 1 x 1)


In hexadecimal, it would be represented internally as 4F9:
4 F 9
0100 1111 1001
(4 x 256 + 15 x 16 + 9 x 1) or
(4 x 16**2 + 15 x 16**1 + 9 x 16**0)


In BCD, it would be represented internally as 1273:
1 2 7 3
0001 0010 0111 0011
With, depending on implementation, possibly a sign digit on the end.


So, where does the number 10032 come in???? I am not trying to be obtuse about this, I'm just confused. If one of you can help me understand, I would appreciate it.

BTW, SJA, I ran your code and got 2730???

Thanks,
Paul
 
Paul,
BDC is hex as far as the real value of the number is concerned and is limited to a max value of 0x99.
If you read a PC memory address containing 2 bytes of BCD
0x34,0x12 (remember intel little endian format).
The PC reads this as 0x1234 which is 4669 dec.
(0x12 * 0x100) + 0x34
So to return to the original BCD representation you need
to turn it into a hex string, Hex(4669).
Then to manipulate it as a number, Val(Hex(4669)).
Does that make sense ?
2730 contains the 273 in Loubee's example and as he has not
come back on the issue I guess there is some way of
decoding what gets returned.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top