I receive a string which can be a number '42' or an hexadecimal number '0x12'. I would like to convert this to a number.
I currently have this very cumbersome function:
function str2num(str) {
if (str.substr(0, 2) == '0x')
return parseInt(substr(2, str), 16)
else
return parseInt(str, 10)
}
Is there a shorter way to do that?