Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  183] [ 7]  / answers: 1 / hits: 30480  / 9 Years ago, sun, september 6, 2015, 12:00:00

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


More From » node.js

 Answers
16

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


[#65168] Thursday, September 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
naomim

Total Points: 344
Total Questions: 95
Total Answers: 114

Location: Wales
Member since Mon, May 17, 2021
3 Years ago
;