TO_HEX

Converts an integer to a hexadecimal representation.

Syntax

TO_HEX( expr ) --> TEXT

Arguments

Parameter

Description

expr

This function accepts INT and BIGINT expressions

Returns

If the input number is of type INT, the return string will be 10 characters long (8 characters for the digits and 2 characters for the “0x” prefix). If the input number is of type BIGINT, the return string will be 18 characters long (16 characters for the digits and 2 characters for the “0x” prefix).

Examples

BIGINT data type

CREATE TABLE cool_numbers(number BIGINT NOT NULL);
INSERT INTO cool_numbers VALUES (-42), (3735928559), (666), (3135097598), (3221229823);
SELECT TO_HEX(number) FROM cool_numbers;

Output:

to_hex
------------------
0xffffffffffffffd6
0x00000000deadbeef
0x000000000000029a
0x00000000baddcafe
0x00000000c00010ff

INT data type

CREATE TABLE cool_numbers(number INT NOT NULL);
INSERT INTO cool_numbers VALUES (-42), (373592855), (666), (313509759), (322122982);
SELECT TO_HEX(number) FROM cool_numbers;

Output:

to_hex
----------
0xffffffd6
0x16449317
0x0000029a
0x12afc77f
0x133334e6