SQLite | SQLite Functions | Converting a BLOB Value to Hexadecimal (hex Function)
The hex function converts a BLOB value to its hexadecimal representation. This article explains how to use the function.
Using the hex Function
The hex function represents a BLOB value in hexadecimal. Its syntax is as follows.
hex(BLOB_value)
The argument is treated as a BLOB value, and the function returns its hexadecimal representation.
A BLOB contains binary data. For example, displaying the binary value 6D as a character produces m, which has the character code 6D. The hex function instead returns that binary data as the string 6D.
–
As an example, use the hex function to represent BLOB values generated by the randomblob function in hexadecimal. For details about randomblob, see Generating a Random BLOB Value (randomblob Function).
select hex(randomblob(2));
sqlite> select hex(randomblob(2));
hex(randomblob(2))
------------------
7F64
sqlite> select hex(randomblob(2));
hex(randomblob(2))
------------------
DB6D
sqlite> select hex(randomblob(2));
hex(randomblob(2))
------------------
2176
sqlite> select hex(randomblob(2));
hex(randomblob(2))
------------------
74D5
sqlite>
Each execution of the SELECT statement displayed the generated BLOB value in hexadecimal.
Because the argument is treated as a BLOB, passing a string returns the character codes corresponding to the string’s characters in hexadecimal.
select hex('Aa');
select hex('ABC');
sqlite> select hex('Aa');
hex('Aa')
----------
4161
sqlite> select hex('ABC');
hex('ABC')
-----------
414243
sqlite>
The character codes for the supplied strings are displayed in hexadecimal.