This documentation is archived and is not being maintained.

Hex Function (Visual Basic)

Visual Studio 2008

Returns a string representing the hexadecimal value of a number.

Public Shared Function Hex( _
   ByVal Number As { Byte | SByte | Short | UShort |
   Integer | UInteger | Long | ULong | Object } _
) As String


Required. Any valid numeric expression or String expression.

Exception type

Error number




Number is not specified.



Number is not a numeric type.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

If Number is not already a whole number, it is rounded to the nearest whole number before being evaluated.

If Number is

Hex returns


Zero (0)

Any numeric value

Up to sixteen hexadecimal characters

You can represent hexadecimal numbers directly by preceding numbers in the proper range with &H. For example, &H10 represents decimal 16 in hexadecimal notation.

This example uses the Hex function to return the hexadecimal value of a number.

Dim TestHex As String 
' Returns 5.
TestHex = Hex(5)
' Returns A.
TestHex = Hex(10)
' Returns 1CB.
TestHex = Hex(459)

Namespace: Microsoft.VisualBasic

Module: Conversion

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)