Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode Base64 string in node.js

I'm trying to decode a base64 string representing an image stored in a db. I tried many libraries and solutions provided on SO, but I'm still unable to decode the image correctly. In particular, using the following code:

var img = new Buffer(b64, 'base64').toString('ascii');

I get a similar binary representation, except for the first bytes. This is the initial part of the base64 string:

/9j/4RxVRXhpZgAASUkqAAgAAAANADIBAgAUAAAAqgAAACWIBAABAAAAiwYAABABAgAIAAAAvgAA

Here are the first 50 bytes of the original image:

ffd8ffe11c5545786966000049492a00080000000d003201020014000000aa00000025880400010000008b06000010010200

And here are the first 50 bytes of the string I get with javascript:

7f587f611c5545786966000049492a00080000000d0032010200140000002a00000025080400010000000b06000010010200

How you can see, the two strings are identical except for the fisrt 3 bytes and some few bytes in the middle.
Can somebody help me understand why this is happening and how to solve it? Thanks

like image 463
SimoV8 Avatar asked Sep 06 '15 17:09

SimoV8


People also ask

How do you decode Base64?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

What is Base64 Buffer?

The buffer object can be encoded and decoded into Base64 string. The buffer class can be used to encode a string into a series of bytes. The Buffer. from() method takes a string as an input and converts it into Base64. The converted bytes can be changed again into String.

Is JS BTOA deprecated?

btoa and atob are only deprecated for Node JS. If you prepend the window. you will get rid of this deprecation mark. On the other hand, if you are trying to use btoa o atob in the back-end side, you definitely should use Buffer interface.


1 Answers

The problem is that you're trying to convert binary data to ASCII, which most likely than not, will mean loss of data since ASCII only consists of values 0x00-0x7F. So when the conversion takes place, all bytes > 0x7F are capped at 0x7F.

If you do this instead, you can see the data matches your first 50 bytes of the original image:

console.log(Buffer.from(b64, 'base64').toString('hex'));

But if you want to keep the binary data intact, just keep it as a Buffer instance without calling .toString(), as many functions that work with binary data can deal with Buffers (e.g. fs core module).

like image 145
mscdex Avatar answered Sep 22 '22 04:09

mscdex