【发布时间】:2018-03-08 13:48:30
【问题描述】:
我在读取 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 .
$
注意使用节点读取时如何在索引 0x29 处插入 0xc2 字节。这是为什么?我已经将二进制编码声明为readFileSync 和toString。
我也尝试过 ascii,但得到了不同且同样错误的结果。
【问题讨论】:
标签: javascript node.js binary