JS 将整个缓冲区打印到控制台

JS print entire buffer to console

我正在尝试在控制台中查看此缓冲区的内容。如何将整个内容打印到控制台而不被截断,同时仍以十六进制显示。

let fileBuff = Buffer.from(bufData.slice(0,256))
console.log(fileBuff)

<Buffer 02 d8 43 cf c3 9f ff 50 01 1e 22 02 bf e5 00 
00 00 00 00 02 d6 fb a3 e0 04 f0 70 06 
12 d8 30 e0 04 f0 22 02 d7 16 00 00 00 00 00 02 d7 31 90 7f 78 e0 ... 206 more bytes>

我试着把它包装在 process.stdout.write(JSON.stringify(fileBuff) + '\n'); 但问题是没有 JSON.stringify 它只是给出了一堆没有十六进制的奇怪字符,而有了 JSON.stringify 它不再显示十六进制,而是十进制值。

根据您的情况,使用 Buffer.toString() 方法将缓冲区转换为具有指定编码(“十六进制”)的字符串。

buff = Buffer.from("hello test hello test hello test hello test hello test hello test hello test hello test");
console.log(buff); 
// Output: <Buffer 68 65 6c 6c 6f 20 74 65 73 74 20 68 65 6c 6c 6f 20 74
 65 73 74 20 68 65 6c 6c 6f 20 74 65 73 74 20 68 65 6c 6c 6f 20 74 65 73
 74 20 68 65 6c 6c 6f 20 ... >

console.log(buff.toString("hex"));
// Output: '68656c6c6f20746573742068656c6c6f20746573742068656c6c6f20746573742068656c6
c6f20746573742068656c6c6f20746573742068656c6c6f20746573742068656c6c6f207465737
42068656c6c6f2074657374'

// hex codes ("h": 68, "e": 65, "l": 6c, "o": 6f)