HEX>DEC Convert in XOD?

Hey everyone im here to find if there is a way to accomplish a conversion from hex>dec in XOD, because a LCD library need the LCD addr and without convert it to decimal by yourself i want that user are able to upload directly the hex number or dec number!
More dynamic

The best option for now is just to use a calculator.

For example, https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=0x37

We’ll make custom literals with support for hex and bin a bit latter.

I was trying to create a node that does the hex->dec conversion. The sscanf function makes it pretty trivial:

struct State {
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
auto hex = getValue<input_HEX>(ctx);
int dec;
sscanf(“0x3F”, “0x%x”, &dec);
emitValue<output_DEC>(ctx, dec);
}

But when I replace “0x3F” with hex, it gives a data mis-match. How do I convert XOD string (hex variable) to char* for sscanf?

XOD strings are tricky multi-fragment memory views. There’s a dump function to convert them into plain C-string:

XString hex = getValue<input_HEX>(ctx);
char hexBuff[16] = { 0 }; // unsafe 16 char cap, init with terminal zeros
dump(hex, hexBuff);
// sscanf(hexBuff, ...);

That did the trick. gweimer/utils now has a hex-to-number node for converting a hex string into a number. It expects a leading “0x” as part of the string. Your program will blow up if your hex string is >16 characters long due to a buffer over-flow, but I can’t imagine anyone legitimately using more than 6-8 characters. If I can figure out how to get the length of XString hex, I can remove that bug.

1 Like
XString hex = getValue<input_HEX>(ctx);
size_t len = length(hex);

The funcs are documented in C++ API reference.

Magic I will download that and implement in my LCD library :smiley: Thank you boss