SQLite User Forum

The hex() value is different from the requested value
Login

The hex() value is different from the requested value

(1) By anonymous on 2022-08-05 05:06:58 [link] [source]

When I enter the integer number, say, for example, that it is 101056520 as follows:

SELECT hex(101056520)

The result is 313031303536353230

While when I enter the same number on any online decimal to hexadecimal converter, The result is 6060008, which is the required output. Is there a way to convert the decimal number or the integer number to a hexadecimal number in the required form?

I am using db browser for sqlite

(2) By Stephan Beal (stephan) on 2022-08-05 05:28:48 in reply to 1 [link] [source]

I am using db browser for sqlite

To save someone the effort: the sqlite shell output is the same.

(3) By Gunter Hick (gunter_hick) on 2022-08-05 05:45:14 in reply to 1 [link] [source]

The hex() function DOES NOT convert a decimal integer value into its hexadecimal representation.

The hex() function converts a BLOB into a string of the hex representation of its bytes.

Thus the digits 0..9 convert to the ASCII codepoints 0x30 .. 0x39.

see https://sqlite.org/lang_corefunc.html

"The hex() function interprets its argument as a BLOB and returns a string which is the upper-case hexadecimal rendering of the content of that blob."

(4) By Herbert Gläser (hglaeser) on 2022-08-05 05:45:24 in reply to 1 [link] [source]

https://www.sqlite.org/lang_corefunc.html#hex

hex(X)

The hex() function interprets its argument as a BLOB and returns a string which is the upper-case hexadecimal rendering of the content of that blob.

If the argument X in "hex(X)" is an integer or floating point number, then "interprets its argument as a BLOB" 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".

(5.2) By RandomCoder on 2022-08-05 06:22:46 edited from 5.1 in reply to 1 [link] [source]

If you want to convert, you can use something like select printf("%x", 101056520);

(6) By Gunter Hick (gunter_hick) on 2022-08-05 06:02:15 in reply to 5.0 [source]

Wrong. The hex() function works on BLOBs, not numbers. It has nothing to do with base conversion.

(7) By RandomCoder on 2022-08-05 06:22:22 in reply to 6 [link] [source]

Yep, you're right of course, sorry for the confusion, I've removed it altogether.

(8) By anonymous on 2022-08-05 06:33:17 in reply to 5.2 [link] [source]

Thank you very much, this is exactly what I was looking for

(9) By anonymous on 2022-08-05 06:41:38 in reply to 5.2 [link] [source]

The answer was short and clear, thank you very much