简体   繁体   English

转换缓冲区base64 - > utf8编码node.js

[英]Convert buffer base64 -> utf8 encoding node.js

My app imports all the messages from the Notes folder of GMail. 我的应用程序导入GMail的Notes文件夹中的所有邮件。 I use imap npm module for that. 我使用imap npm模块。

Using the example from their github page I get all the contents of a message into a buffer: 使用他们的github页面中的示例,我将消息的所有内容放入缓冲区:

 stream.on('data', function(chunk) {
     count += chunk.length;
     buffer += chunk.toString('utf8');
 });

However, what I get are sentences like 但是,我得到的是句子

  0KHQvdCw0YfQsNC70LAg0YHQvtC30LTQsNC10YLRgdGPINGA0LXRiNC10YLQutCwINC/0YDQvtGB 0YLRgNCw0L3RgdGC0LLQsCDQstC+0L7QsdGA0LDQttC10L3QuNGPLiZuYnNwOzxkaXY+PGJyPjwv ZGl2PjxkaXY+0JfQsNGC0LXQvCDQvdCwI

(wrong conversion from Russian) (错误的俄语转换)

I found out that these are the snippets of text encoded in base64 and in order to read them I need to convert it from base64 to utf8. 我发现这些是在base64中编码的文本片段,为了阅读它们,我需要将它从base64转换为utf8。

There is also sometimes an annoying = character that appears from nowhere... 有时还会出现恼人的=字符,无处不在......

 letting them f= all on her shoulders

Do you know how I could get rid of those two problems? 你知道我怎么能摆脱这两个问题吗?

Thank you! 谢谢!

new Buffer(...) has been deprecated for a while now, go for Buffer.from(...) new Buffer(...)已经被弃用了一段时间,转到Buffer.from(...)

a simple example might be: 一个简单的例子可能是:

var utf8encoded = Buffer.from(base64encoded, 'base64').toString('utf8');

In order to convert from a base64 encoded String to utf8 you can use the following: 要从base64编码的String转换为utf8,您可以使用以下内容:

var base64encoded = '0KHQvdCw0YfQsNC70LAg0YHQvtC30LTQsNC10YLRgdGPINGA0LXRiNC10YLQutCwINC/0YDQvtGB 0YLRgNCw0L3RgdGC0LLQsCDQstC+0L7QsdGA0LDQttC10L3QuNGPLiZuYnNwOzxkaXY+PGJyPjwv ZGl2PjxkaXY+0JfQsNGC0LXQvCDQvdCwI';

var utf8encoded = (new Buffer(base64encoded, 'base64')).toString('utf8');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM