TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

 Home » Tip Bank » .NET » VB.NET » Math Processing
Language: VB4,VB5,VB6,VBS
Expertise: beginner
Apr 20, 1999

WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

While Visual Basic offers the Hex\$ function that converts a decimal value into its hexadecimal equivalent number, it seems that the inverse function is missing. Not true. Try out this one-liner:

``````
Function HexToDec(HexValue As String) As Long
HexToDec = Val("&H" & HexValue)
End Function
``````
UPDATE The code in the original tip always tries to return an Integer value, which means that, for example, "FFFF" is converted to -1 instead of 65,535. The following new versions fixes this behavior, while still giving you the capability to convert to an integer:
``````
Function HexToDec(HexValue As String, Optional ToInteger As Boolean) As Long
If ToInteger Then
' convert to an integer value, if possible.
' Use CInt() if you want to *always* convert to an Integer
HexToDec = Val("&H" & HexValue)
Else
' always convert to a Long. You can also use the CLng() function.
HexToDec = Val("&H" & HexValue & "&")
End If
End Function
``````
Thanks to Joe Hartman for spotting the problem with the original routine and suggesting the solution.
Francesco Balena

 Submit a Tip Browse ".NET" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date