uint32_t char_to_int(char c){
if(c >='0' && c<='9')
return c -'0';
else
return c - 'a' + 10;
}
This function converts digits ('0' to '9'), to their numeric equivalent i.e. '0' is converted to 0, '1' is converted to 1, .... '9' is converted to 9.
The c - 'a' + 10, converts lower case letters to a numeric equivalent viz 'a' becomes 10, 'b' becomes 11, ..... 'z' becomes 35. However, this ONLY works if the implementation (i.e. compiler) uses a character set with a contiguous set of lower-case letters (like ASCII). There are real-world character sets that do not have a contiguous set of lower-case letters though.
The problem is that conversion is also done for all characters, other than digits, and the result is then probably meaningless. For example, it will not correctly handle uppercase letters (if one expects uppercase letters to be converted to the same as their lowercase equivalents).
The function operates on one character at a time. It attempts to do the same thing as the standard function strtoul() except, except that strtoul() (1) works on a string (not a single character), (2) works correctly for both upper and lower case letters, (3) will work correctly regardless of what character set the implementation supports, and (4) does error checking (e.g. returning zero if an invalid character is detected). strtoul() is not limited to hex conversion - it works up to base 35.
As to an equivalent in JavaScript - essentially the same code will probably work, but with flaws similar to those in the C/C++ version.