SQLite HEX() Function
The SQLite HEX() function interprets the passed argument as a BLOB and returns a upper-case hexadecimal string rendering the content of that blob.
If the argument is an integer or floating point number, then it is interpreted as BLOB which means that the binary number is first converted into a UTF8 text representation, then that text is interpreted as a BLOB. Hence, HEX(12345678) renders as "3132333435363738" not the binary representation of the integer value "0000000000BC614E".
Syntax
HEX(X)
Parameters
X |
Required. Specify the value to convert to a hexadecimal representation. |
Return Value
Returns the hexadecimal representation of a decimal or string value.
Example 1:
The example below shows the usage of HEX() function.
SELECT X'78797A', HEX('xyz'); Result: 'xyz', '78797A' SELECT X'53514C', HEX('SQL'); Result: 'SQL', '53514C' SELECT HEX(255); Result: '323535' SELECT HEX(500); Result: '353030'
Example 2:
Consider a database table called Sample with the following records:
Data | x |
---|---|
Data 1 | 10 |
Data 2 | 20 |
Data 3 | 30 |
Data 4 | 40 |
Data 5 | 50 |
The statement given below can be used to get the hexadecimal representation of values of column x. Please note that it is first converted into a UTF8 text representation, then that text is interpreted as a BLOB. Then it returns the upper-case hexadecimal string rendering the content of that blob.
SELECT *, HEX(x) AS HEX_Value FROM Sample;
This will produce the result as shown below:
Data | x | HEX_Value |
---|---|---|
Data 1 | 10 | 3130 |
Data 2 | 20 | 3230 |
Data 3 | 30 | 3330 |
Data 4 | 40 | 3430 |
Data 5 | 50 | 3530 |
❮ SQLite Functions