ascii-to-hex

gabbapeople/sim7020/ascii-to-hex

Converts a string by changing the characters it contains from ASCII format to Hexadecimal format.
ascii-to-hex
@/ascii-to-hex
Converts a string by changing the characters it contains from ASCII format to Hexadecimal format.
INstring
ascii-to-hex
IN
OUT
OUTstring
To use the node in your project you should have the gabbapeople/sim7020 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 {
    char* hexStr;
    char* str;
    size_t cap;
    CStringView view;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {

    auto state = getState(ctx);
    char hexChars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    auto string = getValue<input_IN>(ctx);
    state->cap = length(string);

    state->str = new char[state->cap + 1];
    dump(string,state->str);

    state->hexStr = new char[2 * state->cap + 1];
    memset(state->hexStr, '\0', 2 * state->cap + 1);
    state->view = CStringView(state->hexStr);

    for(auto i = 0; i < state->cap; i++){
        state->hexStr[2 * i] = hexChars[ ( state->str[i] & 0xF0 ) >> 4 ];
        state->hexStr[2 * i + 1] = hexChars[ ( state->str[i] & 0x0F ) >> 0 ];
    }

    emitValue<output_OUT>(ctx, XString(&state->view));
}