SQLite Forum

To and From Hex Conversion Methods
Login
Hello,

To exercice me on sqlite I made a project with sqlite and technical doc unicode. The target is to have a database with all unicode codepoint simple to query.

So I wanted to parse codepoint : encoded in a string, integer in hexadecimal encoding. I search builtin solution to parse hexadecimal string number into `INTEGER` but it seem it's impossible with builtin tools.

I tried https://sqlite.org/forum/forumpost/dd104572f2833e48?t=h but I dont need unhex from hex blob data so it didn't work well.

Next I tried with app function with better-sqlite3 npm module (Node.js).
```js
function parseIntFromHexaStr(hexastr) {
    return parseInt(hexastr, 16);
}
db.function('parse_int_from_hexa_str', { varargs: false, deterministic: true }, parseIntFromHexaStr);
```
And it work well.

But I prefer my project be sqlite only, so I write a sqlite extension :

```c
// sqlstrtol.c
/*
** Function STRTOL(str, base=10) -> INTEGER
**
** Decodes the str which must be a parsable number by strtol with the base you gave, if no base passed as argument, it will be 10.
*/

#ifdef __cplusplus
extern "C" {
#endif

#ifndef SQLITE_PRIVATE
    #define SQLITE_PRIVATE static
#endif

#include <stdlib.h>
#include <string.h>

#ifdef SQLITE_CORE
    #include "sqlite3.h"
#else
    #ifdef _HAVE_SQLITE_CONFIG_H
        #include "config.h"
    #endif
    #include "sqlite3ext.h"
    SQLITE_EXTENSION_INIT1
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif

static void _strtolFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
{
    char* text;
    int base;
    char* end;

    // check if arguments are between 1 and 2
    if (argc < 1 || argc > 2) {
        sqlite3_result_null(context);
        return;
    }

    // init variables
    text = (char*)sqlite3_value_text(argv[0]);
    if (argc == 2) {
        base = sqlite3_value_int(argv[1]);
    }
    else {
        base = 10;
    }
    
    long value = strtol(text, &end, base); 

    // parse error strtol put text to endPtr
    if (end == text) {
        sqlite3_result_null(context);
        return;
    }

    sqlite3_result_int64(context, value);
}

#ifdef _WIN32
#ifndef SQLITE_CORE
__declspec(dllexport)
#endif
#endif
int sqlite3_sqlstrtol_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi)
{
    SQLITE_EXTENSION_INIT2(pApi);

    return sqlite3_create_function_v2(db, "STRTOL", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS, 0, _strtolFunc, 0, 0, 0);
}

#ifdef __cplusplus
}
#endif
```

So I exposed strtol from stdlib 
https://en.wikibooks.org/wiki/C_Programming/stdlib.h/strtol

I'm not a C developper, so I don't know if it's the best way to parse string to integer with C.

If an active sqlite maintainer pass here : are you opposed to add this kind of function in builtins of sqlite ?
If someone make a push-request to add this (a parseInt method based on strtol or similar) in builtins, is it ok for you ?

I'm curious about C, fossil and sqlite. So if you have time to help me to make a quality push-request for sqlite, I would love to do it.