简体   繁体   English

node.js从utf8文件创建十六进制缓冲区

[英]node.js create hex buffer from utf8 file

I have a .h264 file that looks like this: 我有一个.h264文件,如下所示:

0000 0001 674d 401f 95a0 1001 86c0 5283
0203 2000 007d 0000 1d4c 1080 0000 0001
68ee 3c80 0000 0001 65b8 041f fff6 91f2
86db 23e0 5147 1a83 b8d4 3141 87b1 43b8
e329 5fd3 5937 bf10 485f af32 4e74 6138
5611 33e3 0778 e035 85ca 2c00 9320 ffbb
45be 09f8 2e51 a039 9f8c fe17 d100 0003
0000 0300 0003 02ed 114b 87ea 1054 5a3f
bf5e 0be5 a555 24f9 edf2 72af d9a1 b329
40df 2a96 1a71 d2c7 79d3 a0d2 c13d 06ea
2bdb bf63 f237 8e61 71b1 f1be e923 5565
7714 92f9 8d86 dfc3 1578 79b5 1ef5 ce02
df78 4982 577b f89d 0141 2dcf 0176 a9db
607c 1341 9c61 0fa0 295f 8e18 fd20 00a1

Using a Node.js fs.createReadStream() I get the following: 使用Node.js fs.createReadStream()我得到以下内容:

<Buffer 30 30 30 30 20 30 30 30 31 20 36 37 34 64 20 34 30 31 66 20 39 35 61 30 20 31 30 30 31 20 38 36 63 30 20 35 32 38 33 0a 30 32 30 33 20 32 30 30 30 20 30 ...>

How do I get the the following instead? 我如何获得以下内容? (basically, telling the buffer to read the raw input as hex codes directly): (基本上,告诉缓冲区直接读取原始输入为十六进制代码):

<Buffer 00 00 00 01 67 4d 40 1F ...>

I created a file called t.h264 with the data you provided. 我用你提供的数据创建了一个名为t.h264的文件。 Then, in test.js : 然后,在test.js

var file = require("fs").createReadStream("t.h264", { encoding: "utf8" });
file.pipe(process.stdout);

This produced the output 这产生了输出

0000 0001 674d 401f 95a0 1001 86c0 5283 ...

which appears to be what you want. 这似乎是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM