如何在 JavaScript 中从十六进制转换为 ASCII?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3745666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:58:40  来源:igfitidea点击:

How to convert from Hex to ASCII in JavaScript?

javascript

提问by Q8Y

How to convert from Hexstring to ASCIIstring in JavaScript?

如何在 JavaScript 中将Hex字符串转换为字符串ASCII

Ex:

前任:

32343630 it will be 2460

32343630 将是 2460

回答by Delan Azabani

function hex2a(hexx) {
    var hex = hexx.toString();//force conversion
    var str = '';
    for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}
hex2a('32343630'); // returns '2460'

回答by michieljoris

For completeness sake the reverse function:

为了完整起见,反向函数:

function a2hex(str) {
  var arr = [];
  for (var i = 0, l = str.length; i < l; i ++) {
    var hex = Number(str.charCodeAt(i)).toString(16);
    arr.push(hex);
  }
  return arr.join('');
}
a2hex('2460'); //returns 32343630

回答by 0x8BADF00D

Another way to do it (if you use Node.js):

另一种方法(如果您使用 Node.js):

var input  = '32343630';
var output = new Buffer(input, 'hex');
log(input + " -> " + output);  // Result: 32343630 -> 2460

回答by Sampath Liyanage

You can use this..

你可以用这个..

var asciiVal = "32343630".match(/.{1,2}/g).map(function(v){
      return String.fromCharCode(parseInt(v, 16));
    }).join('');
    
document.write(asciiVal);

回答by Ayushya

I found a useful function present in web3library.

我在web3库中发现了一个有用的函数。

var hexString = "0x1231ac"
string strValue = web3.toAscii(hexString)

回答by myfavoritenoisemaker

I've found that the above solution will not work if you have to deal with control characters like 02(STX) or 03(ETX), anything under 10will be read as a single digit and throw off everything after. I ran into this problem trying to parse through serial communications. So, I first took the hex string received and put it in a buffer object then converted the hex string into an array of the strings like so:

我发现如果您必须处理02(STX) 或03(ETX) 之类的控制字符,则上述解决方案将不起作用,下面的任何内容都10将被读取为一位数,然后丢弃所有内容。我在尝试通过串行通信解析时遇到了这个问题。因此,我首先将收到的十六进制字符串放入缓冲区对象中,然后将十六进制字符串转换为字符串数组,如下所示:

buf = Buffer.from(data, 'hex');
l = Buffer.byteLength(buf,'hex');
for (i=0; i<l; i++){

    char = buf.toString('hex', i, i+1);
    msgArray.push(char);

}

Then .join it

然后.加入它

message = msgArray.join('');

then I created a hexToAsciifunction just like in @Delan Azabani'sanswer above...

然后我创建了一个hexToAscii函数,就像上面@Delan Azabani 的回答一样......

function hexToAscii(str){
    hexString = str;
    strOut = '';
        for (x = 0; x < hexString.length; x += 2) {
            strOut += String.fromCharCode(parseInt(hexString.substr(x, 2), 16));
        }
    return strOut;    
}

then called the hexToAsciifunction on 'message'

然后hexToAscii在“消息”上调用函数

message = hexToAscii(message);

This approach also allowed me to iterate through the array and slice into the different parts of the transmission using the control characters so I could then deal with only the part of the data I wanted. Hope this helps someone else!

这种方法还允许我遍历数组并使用控制字符将传输的不同部分切片,这样我就可以只处理我想要的数据部分。希望这对其他人有帮助!

回答by SimonC

An optimized version of the implementation of the reverse function proposed by @michieljoris (according to the comments of @Beterraba and @Mala):

@michieljoris 提出的反向函数实现的优化版本(根据@Beterraba 和@Mala 的评论):

function a2hex(str) {
  var hex = '';
  for (var i = 0, l = str.length; i < l; i++) {
    var hexx = Number(str.charCodeAt(i)).toString(16);
    hex += (hexx.length > 1 && hexx || '0' + hexx);
  }
  return hex;
}
alert(a2hex('2460')); // display 32343630

回答by Zibri

console.log(

"68656c6c6f20776f726c6421".match(/.{1,2}/g).reduce((acc,char)=>acc+String.fromCharCode(parseInt(char, 16)),"")

)

回答by mathad

** for Hexa to String**

** 十六进制转字符串**

let input = '32343630';

让输入 = '32343630';

Note : let output = new Buffer(input, 'hex'); // this is deprecated

注意: let output = new Buffer(input, 'hex'); // 这已被弃用

let buf = new Buffer.from(input, "hex");

让 buf = new Buffer.from(input, "hex");

let data = buf.toString("utf8");

让数据 = buf.toString("utf8");