Node.js的缓冲编码问题 [英] Node.js buffer encoding issue

查看:141
本文介绍了Node.js的缓冲编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解node.js中的字符编码我发送数据,由于某种原因,编码将导致某些字符与其他的代替。我正在做的是在​​客户端和它的node.js解码基地64编码。

I'm having trouble understanding character encoding in node.js. I'm transmitting data and for some reason the encoding causes certain characters to be replaced with other ones. What I'm doing is base 64 encoding at the client side and decoding it in node.js.

要简化,我将范围缩小到这块code的这失败:

To simplify, I narrowed it down to this piece of code which fails:

new Buffer("1w==", 'base64').toString('utf8');

1瓦特== 的base 64编码×字符。现在,路过时该字符串与'的base64'参数一个缓冲区,然后做的ToString('UTF8')我有望获得相同的字符回来了,但我没有。相反,我得到了 (字符code 65533 )。

The 1w== is the base 64 encoding of the × character. Now, when passing this string with the 'base64' argument to a buffer and then doing .toString('utf8') I expected to get the same character back, but I didn't. Instead I got (character code 65533).

时的编码 UTF8 错了吗?如果是这样,我应该怎么用呢?如果没有,我怎么能去code在node.js中基64字符串?

Is the encoding utf8 wrong? If so, what should I use instead? If not, how can I decode a base 64 string in node.js?

推荐答案

没有,你的假设是错误的。基于64位恩codeD字符串显然只有一个字节连接codeD。以上U + 007F所有的Uni code code点至少需要两个字节是EN为UTF-8 codeD。

No, your assumption is wrong. The base64-encoded string obviously has only one byte encoded. And all Unicode code points above U+007F need at least two bytes for being encoded in UTF-8.

我还是不擅长记解码的base64,但尽量ISO-8859-1代替。

I'm still not good at decoding base64 in mind, but try ISO-8859-1 instead.

问题的关键是,BASE64解码转换字符串到字节串。你认为它去codeS为字符串,但这是错误的。您仍然需要连接code中的字节串为字符串,并在你的情况下,正确的编码是ISO-8859-1。

The point is, base64 decoding transforms a character string to a byte string. You assumed that it decodes to a character string, but this is wrong. You still need to encode the byte string to a character string, and in your case the correct encoding is ISO-8859-1.

这篇关于Node.js的缓冲编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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