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 will be 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 will be truncated to 16. If the string is longer than minLength without leading characters, then minLength is effectively ignored. Negative numbers may only be represented at minimum by their underlying data size, so for an integer (32-bit) the minLength will be at minimum 8, for a long (64-bit) it will be at minimum 16.

Returns

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

Example

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