dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

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.


Tip of the Day
Language: VB4,VB5,VB6,VBS
Expertise: beginner
Apr 20, 1999

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Convert Hexadecimal numbers

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
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date