8

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

SimoV8
  • 1,382
  • 1
  • 18
  • 32
  • 1
    possible duplicate of [NodeJS: How to decode base64 encoded string back to binary?](http://stackoverflow.com/questions/14573001/nodejs-how-to-decode-base64-encoded-string-back-to-binary) – Vidul Sep 06 '15 at 17:21
  • No, it's not a duplicate. I already tried that solution and doesn't work for me. – SimoV8 Sep 06 '15 at 17:36

1 Answers1

18

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

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • 1
    I thought it was a character encryption problem but I was sure ascii values could arrive till 0xFF. However replacing `.toString('hex')` doesn't work for me (I get a completely different string), but removing the `.toString()` call did the work, thank you! – SimoV8 Sep 06 '15 at 17:47
  • 2
    Please be aware that new Buffer has been depreciated, you should see a warning in your logs: "DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. " In mscdex example above use Buffer.from(b64, 'base64').toString('hex') – user2677034 Dec 22 '19 at 00:02