SQLite Forum

To and From Hex Conversion Methods
Login
There is no builtin to convert a hex string to a number.  You could write your own extension function or a CTE to do that.

Example extension function:

/*
** Convert Text Argument which is hex string into decimal
*/

static void _FromHex(sqlite3_context* context, int argc, sqlite3_value **argv)
{
    const char * src;
    unsigned long long value = 0;

    if (sqlite3_value_type(argv[0]) == SQLITE_TEXT)
    {
        src = sqlite3_value_text(argv[0]);
        if (sqlite3_value_bytes(argv[0]) > 16)
            return;
        while (*src != 0)
        {
            if (!isxdigit(*src))
                return;
            value = value << 4;
            value += (*src > '9' ? ((*src & 0x0f) + 9) : (*src & 0x0f));
            src++;
        }
        sqlite3_result_int64(context, (sqlite_int64)value);
    }
}