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

问题描述 投票:2回答:5

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

示例:输入“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,将结果与预设值进行比较并打印结果,但我想尝试使用循环。我该怎么做?

javascript loops typescript hex decimal
5个回答
6
投票

也许你正在寻找这样的东西,知道它可以用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'));

Alternative

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

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

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

2
投票

只是另一种方式来做到这一点......

// 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'));

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;
}

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;
}

0
投票

JavaScript可以原生计数为十六进制。我发现很难,在一个循环中,它将十六进制转换为十进制,所以为了你的目的,这是伟大的。

0x前置你的十六进制,你可以直接写一个for循环。

例如,我想获得这些unicode字符的十六进制值数组,但我默认获取一个十进制值数组。

这是将unicode hex转换为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
© www.soinside.com 2019 - 2024. All rights reserved.