Share via


Hex Function (Visual Basic) 

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

Parameters

  • Number
    Required. Any valid numeric expression or String expression.

Exceptions

Exception type Error number Condition

ArgumentNullException

5

Number is not specified.

ArgumentException

5

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.

Remarks

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

If Number is Hex returns

Empty

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.

Example

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)

Requirements

Namespace: Microsoft.VisualBasic

Module: Conversion

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

See Also

Tasks

How to: Convert Hexadecimal Strings to Numbers

Reference

Oct Function
Type Conversion Functions
ArgumentNullException