javascript 16进制的字符串 转换为 10进制整数

16进制的字符串 转换为整数

function string_hex2int(hex) {
    var len = hex.length, a = new Array(len), code;
    for (var i = 0; i < len; i++) {
        code = hex.charCodeAt(i);
        if (48<=code && code < 58) {
            code -= 48;
        } else {
            code = (code & 0xdf) - 65 + 10;
        }
        a[i] = code;
    }
    
    return a.reduce(function(acc, c) {
        acc = 16 * acc + c;
        return acc;
    }, 0);
}

 // 15 + 16 * 13 + 256 = 479
console.log(string_hex2int("1df"));

猜你喜欢

转载自blog.csdn.net/fareast_mzh/article/details/83114626