假设我有一个包含字符的 Windows-1252 十六进制值的字符串,我想创建适当的 Unicode 字符:
const asciiHex = '85' //represents hellip
parseInt(asciiHex, 16) //I get 133 as expected
我现在不能执行 String.fromCharCode
,因为它采用 Unicode 代码,而不是 ASCII(在 unicode hellip 中是 8230(十进制))。有人知道任何简单的转换吗?
顺便说一句,我在 Node 6 中执行此操作
最佳答案
你没有提到输入编码:其中字符编码是 \x85
映射到水平省略号?原来那是 Windows-1252,Node.js 不会开箱即用。
可以对它进行编码/解码的模块是windows-1252
.
将十六进制代码转换为 UTF-8 编码字符串:
const windows1252 = require('windows-1252');
let asciiHex = '85';
let utf8text = windows1252.decode( Buffer.from(asciiHex, 'hex').toString('binary') );
console.log( utf8text ); // outputs: …
关于javascript - 在 JavaScript 中将 Windows-1252 十六进制值转换为 Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46095334/