Javascript 在 node.js 中读取二进制数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46441667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading binary data in node.js
提问by Robert Larsen
I'm having problems reading binary data in node.js. This is what I do:
我在 node.js 中读取二进制数据时遇到问题。这就是我所做的:
$ cat test.js
var fs = require('fs'),
binary = fs.readFileSync('./binary', 'binary').toString('binary');
process.stdout.write(binary.substring(0, 48));
$ xxd binary
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
00000010: 0300 3e00 0100 0000 0008 0000 0000 0000 ..>.............
00000020: 4000 0000 0000 0000 10a0 0000 0000 0000 @...............
$ node test.js | xxd
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
00000010: 0300 3e00 0100 0000 0008 0000 0000 0000 ..>.............
00000020: 4000 0000 0000 0000 10c2 a000 0000 0000 @...............
00000030: 00 .
$
Notice how a 0xc2 byte is inserted at index 0x29 when reading with node. Why is that? I've stated binary encoding both to readFileSyncand toString.
I've also tried ascii but then I get a different and equally wrong result.
请注意在使用节点读取时如何在索引 0x29 处插入 0xc2 字节。这是为什么?我已经声明了二进制编码 toreadFileSync和toString. 我也试过 ascii 但后来我得到了一个不同且同样错误的结果。
回答by T.J. Crowder
The 'binary'encoding is an alias for 'latin1', which you clearly don't want when reading non-character data.
该'binary'编码是一个别名'latin1',你清楚地阅读非字符数据时不想要的。
If you want the raw data, don't specify an encoding at all(or supply null)*. You'll get a Bufferinstead of a string, which you'd then want to use directly rather than using toStringon it.
如果您想要原始数据,则根本不要指定编码(或提供null)*。你会得到一个Buffer而不是一个字符串,然后你想直接使用toString它而不是在它上面使用。
* (Some APIs [like fs.watch] also accept 'buffer', but it's not on the list of encodingsand readFileSyncdoesn't say it does. [Thanks Patrickfor providing the list link.])
*(某些 API [如fs.watch] 也接受'buffer',但它不在编码列表中,readFileSync也没有说它确实如此。[感谢帕特里克提供列表链接。])
回答by Patrick Roberts
Just to add some more information, the reason this is happening is because you're passing a string to stdout.write(), which is implicitly converted back into a Bufferbefore being written, and when you do that in the Node.js REPL with this particular substring at position 0x28 of your binary file, you get the behavior you described:
只是为了添加更多信息,发生这种情况的原因是因为您将字符串传递给stdout.write(),Buffer在写入之前将其隐式转换回 a ,并且当您在 Node.js REPL 中使用此特定子字符串执行此操作时0x28 的二进制文件,你会得到你描述的行为:
> new Buffer('\u0010\u00a0')
<Buffer 10 c2 a0>
So as @T.J.Crowder correctly suggested, here's how to fix your script:
正如@TJCrowder 正确建议的那样,以下是修复脚本的方法:
var fs = require('fs'),
binary = fs.readFileSync('./binary');
process.stdout.write(binary.slice(0, 48));
This also uses Buffer#slice()instead of String#substring().
这也使用Buffer#slice()代替String#substring()。

