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 · Runtime · all subjects

binary-data/conversion

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

Convert TypedArray to ArrayBuffer

The buffer property of a TypedArray is the underlying ArrayBuffer. A TypedArray can be a view of a slice of that buffer, so the sizes may differ. Access it with arr.buffer.

Convert TypedArray to DataView

To create a DataView over the same byte range as a TypedArray: new DataView(arr.buffer, arr.byteOffset, arr.byteLength);

Convert TypedArray to Buffer

To convert a TypedArray to Buffer: Buffer.from(arr);

Convert TypedArray to string

To convert a TypedArray to string as UTF-8: new TextDecoder().decode(arr);

Convert TypedArray to number array

To convert a TypedArray to a number array: Array.from(arr);

Convert TypedArray to Blob

To convert a TypedArray to Blob (only if arr is a view of its entire backing TypedArray): new Blob([arr.buffer], { type: "text/plain" });

Convert TypedArray to ReadableStream

To convert a TypedArray to ReadableStream: new ReadableStream({ start(controller) { controller.enqueue(arr); controller.close(); } });. For chunked streaming, split the TypedArray into chunks: new ReadableStream({ start(controller) { for (let i = 0; i < arr.length; i += chunkSize) { controller.enqueue(arr.slice(i, i + chunkSize)); } controller.close(); } });

Convert DataView to ArrayBuffer

To get the underlying ArrayBuffer from a DataView: view.buffer;

Convert DataView to TypedArray

To convert a DataView to TypedArray, only works if the byteLength of the DataView is a multiple of the BYTES_PER_ELEMENT of the TypedArray subclass. Examples: new Uint8Array(view.buffer, view.byteOffset, view.byteLength); new Uint16Array(view.buffer, view.byteOffset, view.byteLength / 2); new Uint32Array(view.buffer, view.byteOffset, view.byteLength / 4);

Convert DataView to string

To convert a DataView to string as UTF-8: new TextDecoder().decode(view);

Convert DataView to number array

To convert a DataView to a number array: Array.from(view);

Convert Buffer to ArrayBuffer

To get the underlying ArrayBuffer from a Buffer: buf.buffer;

Convert Buffer to TypedArray

To convert a Buffer to TypedArray: new Uint8Array(buf);

Convert Buffer to DataView

To convert a Buffer to DataView: new DataView(buf.buffer, buf.byteOffset, buf.byteLength);

Convert Buffer to string

To convert a Buffer to string: buf.toString(); converts as UTF-8. buf.toString("base64"); converts as base64. buf.toString("hex"); converts as hex.

Convert Buffer to number array

To convert a Buffer to a number array: Array.from(buf);

Convert Buffer to Blob

To convert a Buffer to Blob: new Blob([buf], { type: "text/plain" });

Convert Buffer to ReadableStream

To convert a Buffer to ReadableStream: new ReadableStream({ start(controller) { controller.enqueue(buf); controller.close(); } });. For chunked streaming: new ReadableStream({ start(controller) { for (let i = 0; i < buf.length; i += chunkSize) { controller.enqueue(buf.slice(i, i + chunkSize)); } controller.close(); } });

Convert Blob to TypedArray

To convert a Blob to TypedArray: await blob.bytes();

Convert Blob to DataView

To convert a Blob to DataView: new DataView(await blob.arrayBuffer());

Convert Blob to Buffer

To convert a Blob to Buffer: Buffer.from(await blob.arrayBuffer());

Convert Blob to string

To convert a Blob to string as UTF-8: await blob.text();

Convert Blob to number array

To convert a Blob to a number array: Array.from(await blob.bytes());

Convert ReadableStream to Uint8Array

To convert a ReadableStream to Uint8Array using Bun function: Bun.readableStreamToBytes(stream);. Alternatively with Response: new Response(stream).bytes();

Convert ReadableStream to TypedArray

To convert a ReadableStream to TypedArray using Bun function: new Int8Array(Bun.readableStreamToArrayBuffer(stream));. Alternatively with Response: const buf = await new Response(stream).arrayBuffer(); new Int8Array(buf);

Convert ReadableStream to DataView

To convert a ReadableStream to DataView using Bun function: new DataView(Bun.readableStreamToArrayBuffer(stream));. Alternatively with Response: const buf = await new Response(stream).arrayBuffer(); new DataView(buf);

Convert ReadableStream to Buffer

To convert a ReadableStream to Buffer using Bun function: Buffer.from(Bun.readableStreamToArrayBuffer(stream));. Alternatively with Response: const buf = await new Response(stream).arrayBuffer(); Buffer.from(buf);

Convert ReadableStream to string

To convert a ReadableStream to string as UTF-8 using Bun function: await Bun.readableStreamToText(stream);. Alternatively with Response: await new Response(stream).text();

Convert ReadableStream to number array

To convert a ReadableStream to a number array using Bun function: Array.from(new Uint8Array(Bun.readableStreamToArrayBuffer(stream)));. Alternatively with Response: const arr = await new Response(stream).bytes(); Array.from(arr);

Convert ReadableStream to chunk array

To resolve a ReadableStream to an array of its chunks, where each chunk may be a string, typed array, or ArrayBuffer: Bun.readableStreamToArray(stream);

Convert ReadableStream to Blob

To convert a ReadableStream to Blob: new Response(stream).blob();

Give your agent this brain