簡體   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