hex-to-number

gweimer/utils/hex-to-number

Convert a hex string (with leading 0x) into a number. A negative output indicates position of error on invalid input string.
hex-to-number
@/hex-to-number
Convert a hex string (with leading 0x) into a number. A negative output indicates position of error on invalid input string.
HEXstring
hex-to-number
DEC
HEX
DECnumber
To use the node in your project you should have the gweimer/utils library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    auto hex = getValue<input_HEX>(ctx);
    char valid[] = "0123456789ABCDEFabcdef";
    int error = 0;
    int i = 0; // Index for position in string
    for (auto it = hex->iterate(); !error && it; ++it) {
        i++;
        if (i == 1) {
            (char)*it == '0' || (error = -i);
        } else if (i == 2) {
            (char)*it == 'x' || (error = -i);
        } else if (strchr(valid, (char)*it) == NULL) {
            error = -i;
        }
    }
    int dec;
    if (error) {
        dec = error;
    } else {
        char hexBuff[length(hex)+1] = { 0 }; // init with terminal zeros
        dump(hex, hexBuff);
        sscanf(hexBuff, "0x%x", &dec);
    }
    emitValue<output_DEC>(ctx, dec);
}