tohex()

Learn how to use the tohex() function to convert the input value to a hexadecimal string.

Converts input to a hexadecimal string.

Syntax

tohex(value, [, minLength ])

Parameters

NameTypeRequiredDescription
valueint or long✔️The value that is converted to a hex string.
minLengthintThe value representing the number of leading characters to include in the output. Values between 1 and 16 are supported. Values greater than 16 are truncated to 16. If the string is longer than minLength without leading characters, then minLength is effectively ignored. Negative numbers are only represented at minimum by their underlying data size, so for an integer (32-bit) the minLength is at minimum 8, for a long (64-bit) it’s at minimum 16.

Returns

If conversion is successful, result is a string value. If conversion isn’t successful, result is null.

Example

The following example checks whether the tohex() integer conversion results in the expected hexadecimal value.

print
    tohex(256) == '100',
    tohex(-256) == 'ffffffffffffff00', // 64-bit 2's complement of -256
    tohex(toint(-256), 8) == 'ffffff00', // 32-bit 2's complement of -256
    tohex(256, 8) == '00000100',
    tohex(256, 2) == '100' // Exceeds min length of 2, so min length is ignored.

Output

print_0print_1print_2print_3print_04
truetruetruetruetrue