Convert Binary.toString('encode64') back to Binary
Asked Answered
L

2

27

I've seen several tutorial explaining how to convert binary image into encode64 representations:

var image = new Buffer(bl.toString(), 'binary').toString('base64');

My question is, how to return this string representation, back to it's buffer's binary data.

Leavis answered 29/11, 2011 at 4:47 Comment(0)
C
64

This question has some helpful info: How to do Base64 encoding in node.js?

The Buffer class itself does the conversion:

var base64data = Buffer.from('some binary data', 'binary').toString('base64');

console.log(base64data);
// ->  'c29tZSBiaW5hcnkgZGF0YQ=='

var originaldata = Buffer.from(base64data, 'base64');

console.log(originaldata);
// ->  <Buffer 73 6f 6d 65 20 62 69 6e 61 72 79 20 64 61 74 61>

console.log(originaldata.toString());
// ->  some binary data
Cruciform answered 29/11, 2011 at 5:18 Comment(1)
I have a related question that I think you'll know the answer to. I appreciate your help! https://mcmap.net/q/46653/-buffer-from-base64encodedstring-39-base64-39-tostring-39-binary-39-vs-39-utf8-39/470749Droll
B
6

new Buffer is deprecated

From Binary to base64:

const base64data = Buffer.from('some binary data', 'binary').toString('base64');
console.log(base64data);
// ->  'c29tZSBiaW5hcnkgZGF0YQ=='

From base64 to binary:

 const origionalData = Buffer.from('some base64 data', 'base64') 

console.log(origionalData) 

// ->  'c29tZSBiaW5hcnkgZGF0YQ=='
Beatriz answered 30/11, 2018 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.