gpt4 book ai didi

node.js - utf-8 编码的字符串到缓冲区 node.js

转载 作者:行者123 更新时间:2023-12-02 02:18:27 25 4
gpt4 key购买 nike

在 nodejs express 服务器 #1 上,我在 HTTP 响应中从另一个 nodejs 服务器 #2 接收二进制流数据。我想将此数据编码为 base64。我有编码问题。我是这样做的,如下所示。

let result = await axios.post(firmwareDownloadURL, {
id: 'firmware1'
}, {
headers: {
'Content-Type': 'application/json',
}
});
let buff1 = new Buffer.from(result.data);
let base64Firmware1 = buff1.toString('base64');

buff1 的值不正确,因此 base64Firmware1 也是错误的。我通过使用 fs 从我的系统读取固件文件来比较它。

let buff2 = fs.readFileSync('./f1.bin');
let base64Firmware2 = buff3.toString('base64');

buff1buff2 不匹配。

buff <Buffer ef bf bd ef bf bd ef bf bd 00 01 ef bf bd 48 ef bf bd ef bf bd 01 6d 02 08 22 ef bf bd ef bf bd 0a ef bf bd ef bf bd 6e 6d 02 08 ef bf bd 0a ef bf bd ... 612 more bytes>

buff2 <Buffer a7 a3 fe 00 01 8d 48 a7 a6 01 6d 02 08 22 a7 a3 0a a7 a6 6e 6d 02 08 f1 a7 a3 0a a7 a6 f7 6d 02 08 b4 a7 a3 32 a7 a6 14 6e 02 08 39 a7 a3 06 a7 a6 56 ... 307 more bytes>

有趣的是,当我将 buff2 转换为字符串并与 result.data 进行比较时,它们匹配。

if (buff2.toString() === result.data && buff2.toString().length ==  result.data.length) {
console.log('equal');
}

它在控制台上打印相等。请帮助我确定,我错过了什么?

最佳答案

您观察到数据在字符串化时匹配是正确的,因为这正是 Axios 在幕后所做的。问题是 Axios 默认情况下会将响应数据字符串化。在 Node.js 上,这意味着 it calls Buffer.toString('utf8') 生成您所看到的内容。因此,字节被解释为 UTF-8,这将无效代码单元转换为 U+FFFD REPLACEMENT CHARACTER Unicode 代码点,如 documentation for Buffer.toString() 中指定的那样:

If encoding is 'utf8' and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character U+FFFD.

U+FFFD 代码点以 UTF-8 编码为代码单元 EF BF BD,您在数据中看到了这一点。

事后将已经字符串化的缓冲区转换为原始缓冲区是不可能的,因为所有非 UTF8 代码单元都转换为 U+FFFD,这会丢失信息。要解决编码问题,您可以使用 Axios 中的 responseType 选项来指定您想要原始缓冲区:

let res = await axios.post(
firmwareDownloadURL,
{
id: "firmware1",
},
{
headers: {
"Content-Type": "application/json",
},
responseType: "arraybuffer",
}
);
let buff1 = res.data;
let base64Firmware1 = buff1.toString("base64");

Valid options responseType'arraybuffer', 'document', 'json', 'text' 'stream',在浏览器环境下使用Axios时,'blob'


作为旁注,如果您有兴趣了解更多有关 Unicode“代码点”到底是什么的信息,我建议您提供 this article阅读。

关于node.js - utf-8 编码的字符串到缓冲区 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66807052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com