Hex string to hex?


How do you change format from an Hex string to an actual hex value;

out[0] = “0x21”; //hex string

and I want to have

out[0] = 0x21; //hex value



Here’s a function I use a lot. You need to strip off the “0x” part, before passing it.

function hexStrToInt(hexStr){ local total = 0; foreach(n in hexStr) { // convert ascii nibble to 0-15; local nibble = n - '0'; if (nibble > 9) nibble = ((nibble & 0x1f) - 7); total = (total <<4) + nibble; } return total; }

It’s working! Thanks a lot!


You can add

if (hexStr.slice(0, 2) == "0x") hexStr = hexStr.slice(2);

at the start of the function to auto-remove an ‘0x’ if present.