Convert base64 buffer & # 8594; utf8 encoding node.js

My application imports all messages from the Notes GMail folder. For this I use the imap npm module.

Using the example from their github page, I get the entire contents of the message to the buffer:

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

However, I get offers like

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

(incorrect conversion from Russian)

I found out that these are base64 encoded text fragments, and to read them I need to convert it from base64 to utf8.

Sometimes an annoying symbol also appears that appears out of nowhere ...

  letting them f= all on her shoulders 

Do you know how I could get rid of these two problems?

Thanks!

+5
source share
2 answers

To convert 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'); + 0L7QsdGA0LDQttC10L3QuNGPLiZuYnNwOzxkaXY + PGJyPjwv ZGl2PjxkaXY + 0JfQsNGC0LXQvCDQvdCwI'; var base64encoded = '0KHQvdCw0YfQsNC70LAg0YHQvtC30LTQsNC10YLRgdGPINGA0LXRiNC10YLQutCwINC/0YDQvtGB 0YLRgNCw0L3RgdGC0LLQsCDQstC+0L7QsdGA0LDQttC10L3QuNGPLiZuYnNwOzxkaXY+PGJyPjwv ZGl2PjxkaXY+0JfQsNGC0LXQvCDQvdCwI'; var utf8encoded = (new Buffer(base64encoded, 'base64')).toString('utf8'); 
+13
source

new Buffer(...) has long been deprecated, go for Buffer.from(...)

there might be a simple example:

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

Source: https://habr.com/ru/post/1206112/


All Articles