题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
从后往前遍历即可
let len = line.length - 1; let res = 0; let bit = 0; while (len > 1) { let ditLeft = '0'.charCodeAt(0); let ditRight = '9'.charCodeAt(0); let charLeft = 'A'.charCodeAt(0); let charRight = 'F'.charCodeAt(0); let temp = line.charCodeAt(len); if (temp >= ditLeft && temp <= ditRight) { res += (temp - ditLeft) * Math.pow(16, bit); } else if (temp >= charLeft && temp <= charRight) { res += (temp - charLeft + 10) * Math.pow(16, bit) } bit += 1; len -= 1; } console.log(res);