节点缓冲区,从utf8到二进制 [英] Node Buffers, from utf8 to binary

查看:196
本文介绍了节点缓冲区,从utf8到二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个来源接收 utf8 的数据,这个数据最初是二进制形式的(这是一个 Buffer )。我必须将这些数据转换成缓冲区。我很难想出如何做到这一点。

I'm receiving data as utf8 from a source and this data was originally in binary form (it was a Buffer). I have to convert back this data to a Buffer. I'm having a hard time figuring how to do this.

这是一个小样本,显示了我的问题:

Here's a small sample that shows my problem:

var hexString = 'e61b08020304e61c09020304e61d0a020304e61e65';
var buffer1 = new Buffer(hexString, 'hex');

var str = buffer1.toString('utf8');
var buffer2 = new Buffer(str, 'utf8');

console.log('original content:', hexString);
console.log('buffer1 contains:', buffer1.toString('hex'));
console.log('buffer2 contains:', buffer2.toString('hex'));

打印

original content: e61b08020304e61c09020304e61d0a020304e61e65
buffer1 contains: e61b08020304e61c09020304e61d0a020304e61e65
buffer2 contains: efbfbd1b08020304efbfbd1c09020304efbfbd1d0a020304efbfbd1e65

在这里,我希望 buffer2 buffer1 完全相同。

Here, I would like buffer2 to be the exact same thing as buffer1.

如何将 utf8 字符串转换为其原始二进制缓冲区


How can I convert an utf8 string to its original binary Buffer?

推荐答案

由于utf8的工作原理,您不能将二进制数据转换为utf8并再次返回与原始二进制数据相同特别是当无效的utf8字符被替换为 \\\�

You cannot expect binary data converted to utf8 and back again to be the same as the original binary data because of the way utf8 works (especially when invalid utf8 characters are replaced with \ufffd).

你必须使用正确保留的另一种格式数据。这可以是十六进制,base64,二进制或第三方模块提供的其他二进制安全格式。显然,如果可以,你应该把它保存为缓冲区。

You have to use another format that correctly preserves the data. This could be 'hex', 'base64', 'binary', or some other binary-safe format provided by a third-party module. Obviously you should probably keep it as a Buffer if you can.

这篇关于节点缓冲区,从utf8到二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆