Node.JS 如何将 ISO-8859-1 解码为 UTF-8?

Node.JS how to decode ISO-8859-1 into UTF-8?

我正在查询 ISO-8859-1 中的数据库,但由于节点以 UTF8 模式运行,我必须将返回的数据转换为这个特定的 DBMS。

我尝试了 iconv,但我不知道如何获得所需的输出。 例如,当我期望返回 0xe2 0x82 0xac 时,我得到了 0xc2 0x80。

var iconv = require('iconv-lite');

var buffer = Buffer.from([0x80]);
var str = iconv.decode(buffer, 'iso-8859-1');
console.log({str});
console.log(new Buffer(str, 'utf8'));
iconv.encode(new Buffer('€','utf8'),'iso-8859-1');

/*
Which outputs
{ str: '' }
<Buffer c2 80>*/

更新:

感谢上面的评论,我意识到尽管我的数据库中有 "ISO8859_1" 字符集,但 IBEXPERT 正在使用并向我显示 WINDOWS-1252 中的数据(已知作为 ANSI) 编码,这解释了为什么我在他们的 HEX 查看器中看到 0x80。

也许 WINDOWS-1252 以某种方式扩展了 ISO8859_1 字符集?

例如: 运行 下面的代码工作正常: € 已正确解码。

var str = iconv.decode(buffer, 'WINDOWS-1252');
console.log({str});
console.log(new Buffer(str, 'utf8'));
var str2 = iconv.encode(new Buffer('€','utf8'),'WINDOWS-1252');
console.log({strEncoded: str2})
/*
{ str: '€' }
<Buffer e2 82 ac>
{ strEncoded: <Buffer 80> }
* */

奇怪的是,我使用 node-firebirdlib-fbclient 与我的 firebird 数据库通信的数据库查询解析为 UTF8 字符,您可以用 UTF8 表示看符号值是' ',它翻译成0xc2 0x80.

   { idNumber: 1,
     id: 'EUR',
     taxPercentage: 1,
     isDefault: -1,
     accountNumber: null,
     dontUse: false,
     symbol: '' },
  eur: <Buffer c2 80> }

eur:由 console.log(new Buffer(result.symbol,'utf8'))

输出

并使用以下命令将其从 utf8 解码为 'WINDOWS-1252' iconv.decode(Buffer.from(currency.symbol, 'utf8'), 'WINDOWS-1252') returns

... "defaultCurrency":{ "id": "EUR", "symbol": "€", "label": "EUR" }...