new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · all subjects

binary/buffer

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Buffer.toString() method signature

The Buffer class provides a .toString() method to convert a Buffer to a string. The method signature is toString(encoding?: string, start?: number, end?: number). The encoding parameter specifies the character encoding (e.g., 'utf8') and is optional. The start and end parameters specify the byte range to convert and are optional. If encoding is not specified, it defaults to 'utf8'. Start and end define the byte offsets within the Buffer to convert.

Buffer.toString() basic usage

To convert a Buffer to a string using the default UTF-8 encoding, call .toString() with no arguments. Example: const buf = Buffer.from("hello"); const str = buf.toString(); // => "hello"

Buffer.toString() with encoding and byte range

The .toString() method accepts encoding and byte range parameters. Example: const buf = Buffer.from("hello world!"); const str = buf.toString("utf8", 0, 5); // => "hello". The second parameter (0) is the start byte offset, and the third parameter (5) is the end byte offset.

Buffer extends Uint8Array - no conversion needed

The Node.js Buffer class extends Uint8Array, so no conversion is needed when working with Bun. All properties and methods on Uint8Array are available on Buffer. Buffer instances pass instanceof checks for Uint8Array.

Buffer instanceof Uint8Array returns true

A Buffer instance created with Buffer.alloc() will return true when checked with instanceof Uint8Array.

Convert Uint8Array to Buffer using Buffer.from()

The Buffer class extends Uint8Array with additional methods. To create a Buffer instance from a Uint8Array, use Buffer.from() and pass the Uint8Array as the argument.

Convert Uint8Array to Buffer code example

const arr: Uint8Array = ... const buf = Buffer.from(arr);

Read file to Buffer using arrayBuffer()

To read a file into a Buffer in Bun, first read it as an ArrayBuffer using the .arrayBuffer() method on a BunFile instance, then pass the result to Buffer.from() to convert it to a Buffer.

Read file to Buffer example

const path = "/path/to/package.json"; const file = Bun.file(path); const arrbuf = await file.arrayBuffer(); const buffer = Buffer.from(arrbuf);

Give your agent this brain