How to convert Hex to Dec? 
Author Message
 How to convert Hex to Dec?

Hi Groups!
I have a question converting a hexadecimal string to decimal value, so if we
pass a "A" to that function, it will return "10" which is the decimal value
for hex "A".  Please help!  thank you.



Mon, 24 Feb 2003 03:31:04 GMT  
 How to convert Hex to Dec?

Public Function Hex2Dec(value As String) As Long

    If UCase(Left(value, 2)) <> "&H" Then value = "&H" & value
    Hex2Dec = Val(value)

End Function

--
Attitudes are contagious. Is yours worth catching?
http://www.acadx.com


Quote:
> Hi Groups!
> I have a question converting a hexadecimal string to decimal value, so if
we
> pass a "A" to that function, it will return "10" which is the decimal
value
> for hex "A".  Please help!  thank you.



Mon, 24 Feb 2003 03:51:54 GMT  
 How to convert Hex to Dec?

Alan,

Use the Val function.

(eg.  Val("&HA")  returns 10
        Val("&H1CB") returns 459 )

---
Hope this helps,
Mark

Quote:

> Hi Groups!
> I have a question converting a hexadecimal string to decimal value, so if we
> pass a "A" to that function, it will return "10" which is the decimal value
> for hex "A".  Please help!  thank you.



Mon, 24 Feb 2003 04:11:19 GMT  
 How to convert Hex to Dec?
Alan,

In most cases, the following code will do the trick:

    Dim s As String
    Dim x As Long
    s = "A"
    x = CLng("&H" & s)

However, since VB uses signed Integers and Longs you can run into trouble with
the above depending on the source of the Hex value and/or its size.  For
example, if you use the VB Hex finction to convert the Integer -1 into Hex you
get "FFFF".  If you then convert this back using the above formula you will get
65535.  The best way to avoid this to always use Longs.

Also if your Hex string comes from a system that uses unsigned integers/longs
you could get wrong answers.

Regards,

John.............

Quote:

> Hi Groups!
> I have a question converting a hexadecimal string to decimal value, so if we
> pass a "A" to that function, it will return "10" which is the decimal value
> for hex "A".  Please help!  thank you.



Mon, 24 Feb 2003 04:51:38 GMT  
 How to convert Hex to Dec?
Alan,
The old soldier "Val" is what you're looking for.  It works in the same way as
putting hex constants in your own code.
Try this :

nNumber = Val( "&H" & sHexString )

HTH,
    Phill  W.

Quote:

> Hi Groups!
> I have a question converting a hexadecimal string to decimal value, so if we
> pass a "A" to that function, it will return "10" which is the decimal value
> for hex "A".  Please help!  thank you.



Mon, 24 Feb 2003 16:49:16 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How do I convert hex to dec?

2. Need help - converting bin / hex /dec

3. How to convert from hex to dec?

4. How do I convert Hex-values to Dec?

5. Converting Hex/Oct/Bin values into Dec

6. Convert Number from HEX to DEC

7. Converting Dec to Bin and Bin to Dec.

8. Converting HEX without parity to HEX with even parity

9. Converting hex value in str format to hex

10. Hex to Dec

11. dec to hex

12. Hex to Dec

 

 
Powered by phpBB® Forum Software