最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Decode Base64 string in node.js - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked Sep 6, 2015 at 17:06 SimoV8SimoV8 1,4021 gold badge19 silver badges35 bronze badges 2
  • 1 possible duplicate of NodeJS: How to decode base64 encoded string back to binary? – Vidul Commented Sep 6, 2015 at 17:21
  • No, it's not a duplicate. I already tried that solution and doesn't work for me. – SimoV8 Commented Sep 6, 2015 at 17:36
Add a comment  | 

1 Answer 1

Reset to default 19

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

发布评论

评论列表(0)

  1. 暂无评论