Node.js theorytheory 0/50 · 0%
Data · easy

8. Buffers and binary data

Working with raw bytes — hashes, signatures and hex strings.

A `Buffer` is Node's fixed-length array of raw bytes, essential once you touch files, sockets, or blockchain data like transaction hashes, signatures and ABI-encoded calldata (all of which are really just bytes).

js
const buf = Buffer.from("hello", "utf8");
console.log(buf);                 // <Buffer 68 65 6c 6c 6f>
console.log(buf.toString("hex")); // "68656c6c6f"
console.log(buf.length);          // 5 bytes

const hash = Buffer.from("deadbeef", "hex");
console.log(hash.toString("base64"));

Buffers implement the same API as JavaScript's standard `Uint8Array`, so code written for browsers using `Uint8Array` (common in web3 libraries) interoperates with Node's `Buffer` in most cases.

Check your understanding

  1. 1. What does a Buffer represent?

  2. 2. Which encoding turns bytes into a compact printable string often used for hashes?

  3. 3. What browser type is closely compatible with Buffer?