DOCUMENT:Q161304 21-MAR-1997 [vbwin] TITLE :HOWTO: Convert Hexadecimal Numbers to Long Integer PRODUCT :Microsoft Visual Basic for Windows PROD/VER:2.0 3.0 4.0 5.0 OPER/SYS:WINDOWS KEYWORDS:kbusage PrgOther vb5all vb5howto VBKBVBA kbhowto ====================================================================== -------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Control Creation, Learning, Professional and Enterprise Editions for Windows, version 5.0 - Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0 - Microsoft Visual Basic Standard, Professional and Enterprise Editions for Windows, version 4.0 -------------------------------------------------------------------------- SUMMARY ======= Microsoft Visual Basic has a Hex$() function to convert a number into a string of Hexadecimal digits. However, it has no obvious function to perform the inverse conversion. This article details how to use the Val() function to perform the Hexadecimal to Long Integer conversion, plus a trap to avoid. MORE INFORMATION ================ The Val() function can be used to convert a string of decimal digits into a number. It can also be used on a string of hexadecimal digits, but you have to append the characters "&H" to the string of digits so that the Val() function will use the correct number base in the conversion process. For example: A = Val("1234") ' performs decimal conversion A = Val("7FFF") ' results in 7 - the F's are ignored A = Val("&H7FFF") ' performs hexadecimal conversion Integer Trap ------------ If you are assigning the results of the conversion to a Long integer variable, you will probably only want numbers in the range 80000000 to FFFFFFFF to be treated as negative numbers. However, without taking special precautions, numbers in the range 8000 to FFFF will also be treated as negative. This occurs because numbers with four or fewer digits are converted to signed 2-byte integers. When the signed integer intermediate value is converted to a Long (or 4-byte) integer, the sign is propagated. Visual Basic 4.0/Access 95 and Later ------------------------------------ By appending the "&" Long suffix to the hexadecimal string, even small numbers are treated as Long. The following function performs the conversion correctly: Function HexToLong(ByVal sHex As String) As Long HexToLong = Val("&H" & sHex & "&") End Function Visual Basic 3.0/Access 2.0 and Earlier --------------------------------------- The method illustrated above will not work in Microsoft Visual Basic 3.0 or earlier for hexadecimal numbers greater or equal to 80000000. Please see the REFERENCES section of this article for more information. Because this problem does not occur with numbers that fall in the Integer Trap range, the alternate function given below adds error trapping to try again without the appended "&" Long suffix: Function HexToLong(ByVal sHex As String) As Long On Error Resume Next HexToLong = Val("&H" & sHex & "&") If Err Then On Error Goto 0 HexToLong = Val("&H" & sHex) End If End Function Usage of either version of the function is as follows: A = HexToLong("8000") REFERENCES ========== Microsoft Visual Basic Help topic: Val Function Microsoft Visual Basic 4.0 ReadMe topic: Coercion of Hexadecimal Values For more information, please see the following article in the Microsoft Knowledge Base: ARTICLE-ID: Q95431 TITLE : FIX: Type Mismatch Error If Use VAL Function on Big Value ====================================================================== Keywords : kbusage PrgOther vb5all vb5howto VBKBVBA kbhowto Version : 2.0 3.0 4.0 5.0 Platform : WINDOWS Issue type : kbhowto ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1997.