简体   繁体   中英

JS print entire buffer to console

I'm attempting to look at the contents of this buffer in the console. How do I print the entire thing to the console without it being truncated while still displaying in hex.

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>

I tried wrapping it in process.stdout.write(JSON.stringify(fileBuff) + '\\n'); but the problem with that is without JSON.stringify it just gives a bunch of weird characters without the hex and with the JSON.stringify it no longer shows the hex, but decimal values.

Use Buffer.toString() method to convert buffer into string with specified encoding ("hex") in your case.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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