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

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?

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

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

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

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

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