繁体   English   中英

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)

暂无
暂无

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

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