Hex, Hex$

Returns a string representing the hexadecimal value of a number.

Syntax:

  • Hex$( number )
  • Hex( number )
number
required Any valid numeric or string expression. If number is not a whole number, it is rounded to the nearest whole number before being evaluated.

The $-suffixed form returns a String; the unsuffixed form returns a Variant (String).

number can be any number in the range of a LongLong. The result has no leading zeros. A negative number gives its two’s-complement form at the width of its data type:

If number is Hex returns
Zero or a positive number The digits of its value, up to 16 characters
A negative Integer 4 characters
A negative Long 8 characters
A negative LongLong, Single, Double, Currency or Decimal, or a string holding a negative number 16 characters
Null Null (unsuffixed form only)
Empty Zero ("0")

Note

VB6’s Hex takes only a number in the range of a Long, and raises error 6, Overflow, for any other. It gives a negative Single, Double, Currency or Decimal, or a string holding a negative number, the eight characters of a Long: Hex(-1#) is "FFFFFFFF" in VB6 and "FFFFFFFFFFFFFFFF" in twinBASIC. 64-bit VBA takes the range of a LongLong and gives the 16 characters, as twinBASIC does.

Note

A Single or Double of 2^62 (4,611,686,018,427,387,904) or more, or less than -2^62, raises error 6, Overflow, though its value fits a LongLong, as in 64-bit VBA. A LongLong, Decimal or string of the same value is converted; so is Hex(CLngLng(x)).

For the opposite of Hex, precede a hexadecimal value with &H. For example, Hex(255) returns the string "FF" and &HFF returns the number 255.

Common Errors

Error When
6 (&H6), Overflow number is outside the range of a LongLong, or is a Single or Double less than -4,611,686,018,427,387,904 or greater than 4,611,686,018,427,387,903
13 (&HD), Type mismatch number is a string that does not hold a number (a zero-length string included)
94 (&H5E), Invalid use of Null Hex$ is given a number that is Null

Example

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

Dim MyHex
MyHex = Hex(5)      ' Returns "5".
MyHex = Hex(10)     ' Returns "A".
MyHex = Hex(459)    ' Returns "1CB".

See Also