我有一个ISO-2020-JP编码文本的Buffer
,我需要将其转换为UTF-8。我在NPM上找到了一些库,比如:
- encoding-japanese
- iconv-lite
但是我不知道,也找不到,到底哪个编码对应的是ISO-2020-JP。请建议。
你可以使用encoding.js.
const Encoding = require('encoding-japanese');
const buffer = [27, 36, 66, 36, 51, 36, 115, 36, 75, 36, 65, 36, 79, 27, 40, 74]; // こんにちは in ISO-2022-JP
// if you want to convert an ISO-2022-JP byte array to a UTF-8 byte array:
const utf8Array = Encoding.convert(buffer, {
to: 'UTF8',
from: 'JIS'
});
console.log(utf8Array);
// if you want to convert ISO-2022-JP byte array to JavaScript string:
const unicodeArray = Encoding.convert(buffer, {
to: 'UNICODE',
from: 'JIS'
});
console.log(Encoding.codeToString(unicodeArray));