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.
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
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
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>
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 "got it".
First, let's start with the decimal number 1273.
In an ASCII string, it would be represented internally as "1273":
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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.