如何在JavaScript中使用LOOP将十六进制转换为十进制

时间:2016-11-08 20:29:33

标签: javascript loops typescript hex decimal

是否可以通过循环将十六进制数转换为十进制数?

示例:输入" FE"输出" 254"

我看了这些问题:

How to convert decimal to hex in JavaScript?

Writing a function to convert hex to decimal Writing a function to convert hex to decimal

Writing a function to convert hex to decimal

How to convert hex to decimal in R

How to convert hex to decimal in c#.net?

还有一些与JS或循环无关的东西。我也在寻找其他语言的解决方案,以防我找到方法,但我没有。第一个是最有用的一个。也许我可以使用16,将结果与预设值进行比较并打印结果,但我想尝试使用循环。我该怎么做?

5 个答案:

答案 0 :(得分:5)

也许你正在寻找这样的东西,知道它可以用一个oneliner(parseInt)来完成?

function hexToDec(hex) {
    var result = 0, digitValue;
    hex = hex.toLowerCase();
    for (var i = 0; i < hex.length; i++) {
        digitValue = '0123456789abcdefgh'.indexOf(hex[i]);
        result = result * 16 + digitValue;
    }
    return result;
}

console.log(hexToDec('FE'));

替代

也许您想要使用reduce和ES6 arrow functions

function hexToDec(hex) {
    return hex.toLowerCase().split('').reduce( (result, ch) =>
        result * 16 + '0123456789abcdefgh'.indexOf(ch), 0);
}

console.log(hexToDec('FE'));

答案 1 :(得分:2)

另一种方法......

&#13;
&#13;
// The purpose of the function is to convert Hex to Decimal.  
// This is done by adding each of the converted values.
function hextoDec(val) {
  
    // Reversed the order because the added values need to 16^i for each value since 'F' is position 1 and 'E' is position 0
    var hex = val.split('').reverse().join('');
  
    // Set the Decimal variable as a integer
    var dec = 0;
  
    // Loop through the length of the hex to iterate through each character
    for (i = 0; i < hex.length; i++) {
      
        // Obtain the numeric value of the character A=10 B=11 and so on..
        // you could also change this to var conv = parseInt(hex[i], 16) instead
        var conv = '0123456789ABCDEF'.indexOf(hex[i]);
      
        // Calculation performed is the converted value * (16^i) based on the position of the character
        // This is then added to the original dec variable.  'FE' for example
        // in Reverse order [E] = (14 * (16 ^ 0)) + [F] = (15 * (16 ^ 1)) 
        dec += conv * Math.pow(16, i);
      
    }
  
    // Returns the added decimal value
    return dec;
}

console.log(hextoDec('FE'));
&#13;
&#13;
&#13;

答案 2 :(得分:1)

抱歉这是倒退,我找不到编辑答案的位置,所以这里有更正的答案:

function doit(hex) {
    var num = 0;
    for(var x=0;x<hex.length;x++) {
        var hexdigit = parseInt(hex[x],16);
        num = (num << 4) | hexdigit;
    }
    return num;
}

答案 3 :(得分:0)

如果你想循环每个十六进制数字,那么只需从头到尾循环,在你添加它们时将每个数字4位向左移位(每个十六进制数字是4位长):

function doit(hex) {
    var num = 0;
    for(var x=0;x<hex.length;x++) {
        var hexdigit = parseInt(hex[x],16);
        num = (num << 4) | hexdigit;
    }
    return num;
}

答案 4 :(得分:0)

JavaScript可以自然地以十六进制计数。我发现很难在循环中将十六进制转换为十进制,因此对于您而言,这很棒。

0x开头,您可以直接编写for循环。

例如,我想获取这些Unicode字符的十六进制值数组,但是默认情况下,我要获取十进制值的数组。

这里的示例代码将Unicode十六进制转换为dec

    var arrayOfEmojis = [];

    // my range here is in hex format
    for (var i=0x1F600; i < 0x1F64F; i++) {
        arrayOfEmojis.push('\\u{' + i + '}');
    }

    console.log(arrayOfEmojis.toString()); // this outputs an array of decimals