16

My app imports all the messages from the Notes folder of GMail. I use imap npm module for that.

Using the example from their github page I get all the contents of a message into a buffer:

 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.

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!

4
  • 3
    In order to convert from base64 to utf8 you can use (new Buffer(original, 'base64')).toString('utf8'). Could you provide more details about the = problem? Commented Nov 3, 2014 at 20:23
  • @jabclab this works, but only for the buffers that contain russian only. if there's a buffer that has only english text or english and russian, it produces gibberish. i guess i need to run some script before the conversion to identify the encoding, right? Commented Nov 3, 2014 at 20:32
  • @jabclab regarding the = problem – for some reason sometimes the = sign just gets inserted randomly in the text. i can't seem to find where exactly it appears... thank you! Commented Nov 3, 2014 at 20:33
  • @jabclab could you pls post it as an answer as that worked really well for me? Commented Nov 4, 2014 at 20:41

2 Answers 2

38

new Buffer(...) has been deprecated for a while now, go for Buffer.from(...)

a simple example might be:

var utf8encoded = Buffer.from(base64encoded, 'base64').toString('utf8');
Sign up to request clarification or add additional context in comments.

1 Comment

This only works if base64encoded is a string. If it's already a buffer you get the same encoded value out as what went in. Only thing I've found is to dump the already-encoded buffer to a base64, read it back into the Buffer.from(), and dump it back out as, finally, the plaintext string.
26

In order to convert from a base64 encoded String to utf8 you can use the following:

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

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

1 Comment

new Buffer() is deprecated with Stability:0, see mido's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.