【简单】算法nodeJs:进制转换

97 阅读1分钟

image.png

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    line = await readline();
    line = line.slice(2);
    const map = new Map([
        ["0", 0],
        ["1", 1],
        ["2", 2],
        ["3", 3],
        ["4", 4],
        ["5", 5],
        ["6", 6],
        ["7", 7],
        ["8", 8],
        ["9", 9],
        ["A", 10],
        ["B", 11],
        ["C", 12],
        ["D", 13],
        ["E", 14],
        ["F", 15],
    ]);
    let num = 0;
    for (let i = 0; i < line.length; i++) {
        let ji = 1;
        if (i != line.length - 1) {
            for (let j = 0; j < line.length-1 - i; j++) {
                ji *= 16;
            }
        }
        num += map.get(line[i]) * ji;
    }
    console.log(num);
})();