节点二进制Buffer.toString(),转换回Buffer

时间:2016-01-25 22:28:29

标签: node.js encoding

我使用了一个小型库节点爬虫(https://github.com/sylvinus/node-crawler),它允许我下载二进制文件。问题是它确实将响应缓冲区转换为此行https://github.com/sylvinus/node-crawler/blob/6822559cd2b49aa69e39ff07d61ca92b76fe03ca/lib/crawler.js#L434

中的字符串
response.body = response.body.toString(); //response.body was a Buffer

我在回调中获取了response对象,并尝试将其转换回具有正确值的缓冲区。

原创(在调用.toString()之前)

<Buffer ff d8 ff e1 00 18 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 00 00 00 00 00 00 00 00 ff ec 00 11 44 75 63 6b 79 00 01 00 04 00 00 00 4b 00 00 ff e1 03 ... >

我试过解码&#39;它跟随但没有运气:

Buffer(response.body, 'binary')

<Buffer fd fd fd fd 00 18 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 00 00 00 00 00 00 00 00 fd fd 00 11 44 75 63 6b 79 00 01 00 04 00 00 00 4b 00 00 fd fd 03 ... >

new Buffer(response.body)

<Buffer ef bf bd ef bf bd ef bf bd ef bf bd 00 18 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 00 00 00 00 00 00 00 00 ef bf bd ef bf bd 00 11 44 75 63 6b 79 00 ... >

所以我的问题是:如何从该字符串表示中找回正确的原始缓冲区?

1 个答案:

答案 0 :(得分:0)

你做不到。这显然是known bug。您可以分叉并修复它并将PR提交回原始回购和/或至少表达您对先前链接问题的意见。