Decode Base64 string in node.js

SimoV8 picture SimoV8 · Sep 6, 2015 · Viewed 21.7k times · Source

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

Answer

mscdex picture mscdex · Sep 6, 2015

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).