Last active
July 31, 2025 13:52
-
-
Save ChiChou/97a53caa2c0b49c1991e to your computer and use it in GitHub Desktop.
SQLite3 convert hex string to int (requires sqlite >= 3.8.3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH RECURSIVE | |
| unhex(str, val, weight) AS ( | |
| SELECT 'deadbeef', 0, 1 | |
| UNION ALL | |
| SELECT | |
| substr(str, 1, length(str) - 1), | |
| val + (instr('0123456789ABCDEF', substr(str, length(str), 1)) - 1) * weight, | |
| weight * 16 | |
| FROM unhex WHERE length(str) > 0 | |
| ) | |
| SELECT val FROM unhex order by weight desc limit 1; |
It's as easy as hexstr->>'$' using the JSON features introduced by version 3.38.0 (2022-02-22).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Utilizing ChatGPT-4 alongside this ChiChou snippet, I achieved a result akin to the following:
It's a monster, but it works.
In my case, I have a column UUID v7 with has a timestamp inside, so I need to extract it with
replace(substr(uuid, 0, 14), '-', '')