FirebirdSQL logo
Hexadecimal Notation for Numbers

Integer values can also be entered in hexadecimal notation.Numbers with 1-8 hex digits will be interpreted as type INTEGER;numbers with 9-16 hex digits as type BIGINT;numbers with 17-32 hex digits as type INT128.

Syntax
0{x|X}<hexdigits>

<hexdigits>  ::=  1-32 of <hexdigit>
<hexdigit>   ::=  one of 0..9, A..F, a..f
Examples
select 0x6FAA0D3 from rdb$database           -- returns 117088467
select 0x4F9 from rdb$database               -- returns 1273
select 0x6E44F9A8 from rdb$database          -- returns 1850014120
select 0x9E44F9A8 from rdb$database          -- returns -1639646808 (an INTEGER)
select 0x09E44F9A8 from rdb$database         -- returns 2655320488 (a BIGINT)
select 0x28ED678A4C987 from rdb$database     -- returns 720001751632263
select 0xFFFFFFFFFFFFFFFF from rdb$database  -- returns -1
Hexadecimal Value Ranges
  • Hex numbers in the range 0 …​ 7FFF FFFF are positive INTEGERs with values between 0 …​ 2147483647 decimal.To coerce a number to BIGINT, prepend enough zeroes to bring the total number of hex digits to nine or above.That changes the type but not the value.

  • Hex numbers between 8000 0000 …​ FFFF FFFF require some attention:

    • When written with eight hex digits, as in 0x9E44F9A8, a value is interpreted as 32-bit INTEGER.Since the leftmost bit (sign bit) is set, it maps to the negative range -2147483648 …​ -1 decimal.

    • With one or more zeroes prepended, as in 0x09E44F9A8, a value is interpreted as 64-bit BIGINT in the range 0000 0000 8000 0000 …​ 0000 0000 FFFF FFFF.The sign bit is not set now, so they map to the positive range 2147483648 …​ 4294967295 decimal.

    Thus, in this range, and for 16 vs 16+ digits, prepending a mathematically insignificant 0 results in a different value.This is something to be aware of.

  • Hex numbers between 0 0000 0001 …​ 7FFF FFFF FFFF FFFF are all positive BIGINT.

  • Hex numbers between 8000 0000 0000 0000 …​ FFFF FFFF FFFF FFFF are all negative BIGINT.

  • Hex numbers between 0 0000 0000 0000 0001 …​ 7FFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF are all positive INT128

  • Hex numbers between 8000 0000 0000 0000 0000 0000 0000 0000 …​ FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF are all negative INT128

  • A SMALLINT cannot be written in hex, strictly speaking, since even 0x0 and 0x1 are evaluated as INTEGER.However, if you write a positive integer within the 16-bit range 0x0000 (decimal zero) to 0x7FFF (decimal 32767) it will be converted to SMALLINT transparently.

    It is possible to write to a negative SMALLINT in hex, using a 4-byte hex number within the range 0xFFFF8000 (decimal -32768) to 0xFFFFFFFF (decimal -1).