Hex

From Xojo Documentation

Method

Hex(value as Integer) As String

Supported on Desktop,Web,Console.

Returns as a String the hexadecimal version of the number passed.

Syntax

result=Hex(value)

Part Type Description
result String Value converted to hexadecimal.
value Integer The number to be converted to hexadecimal.

Notes

If the value is not a whole number, the decimal value will be truncated.

You can specify binary, hex, or octal numbers by preceding the number with the & symbol and the letter that indicates the number base. The letter b indicates binary, h indicates hex, and o indicates octal.

VB Compatibility Note: VB rounds the value to the nearest whole number so the Hex function will probably be changed in a future release to do this as well.

Examples

Below are examples of various numbers converted to hex:

Var hexVersion As String
hexVersion = Hex(5) // returns "5"
hexVersion = Hex(75) // returns "4B"
hexVersion = Hex(256) // returns "100"

Formatting a hexadecimal number

It is often useful to keep the same number of digits to represent a hexadecimal number, e.g. 8 characters for a 32 bits hexadecimal number. You can achieve such a task with the following code:

Function Hex32(value As Int32) As String
Const prefix = "00000000" // A 32-bits value is 8 digits long at max

Return Right(prefix + Hex(value), 8) // We always return 8 characters
End Function

For an Int64 integer, you can adapt it as:

Function Hex32(value As Int64) As String
Const prefix="0000000000000000" // A 64-bits value is 16 digits long at max

Return Right(prefix + Hex(value), 16) // We always return 16 characters
End Function

Converting a hexadecimal string to a number

See example in &h.

See Also

&b, &c, &h, &o, &u literals; Bin, DecodeHex, EncodeHex, Oct, Integer.FromHex, Integer.ToHex functions.