You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JavaScript中将ASCII十六进制字符串转换为字符?

ASCII双位十六进制字符串转对应字符的实现方法

针对你提到的双位十六进制格式(如6E、26)转ASCII字符的需求,以下是几种常用编程语言的实现方案:

Python 实现

将空格分隔的十六进制片段逐个转换为十进制整数,再映射为对应ASCII字符:

hex_input = "6E 26 45"
# 分割字符串→转十进制→转字符→拼接结果
output = ''.join([chr(int(hex_segment, 16)) for hex_segment in hex_input.split()])
print(output)  # 输出: n&E

JavaScript 实现

通过parseInt解析十六进制值,再用String.fromCharCode生成对应字符:

const hexInput = "6E 26 45";
const output = hexInput.split(' ')
  .map(hex => String.fromCharCode(parseInt(hex, 16)))
  .join('');
console.log(output); // 输出: n&E

Java 实现

利用Integer.parseInt转换十六进制到十进制,再强制转换为字符:

public class HexToAsciiConverter {
    public static void main(String[] args) {
        String hexInput = "6E 26 45";
        StringBuilder result = new StringBuilder();
        
        for (String hexSegment : hexInput.split(" ")) {
            int decimalValue = Integer.parseInt(hexSegment, 16);
            result.append((char) decimalValue);
        }
        
        System.out.println(result.toString()); // 输出: n&E
    }
}

手动验证逻辑

单个双位十六进制的转换逻辑很简单:比如6E转十进制是110,对应ASCII表中编号110的字符就是n;26转十进制是38,对应&,和你的示例完全匹配。

内容的提问来源于stack exchange,提问作者Leo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:20:28