aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/binary/arraybuffer-to-array.md
diff options
context:
space:
mode:
authorGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-07-26 14:59:39 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-26 14:59:39 -0700
commit4c89c60867591b50e0b31bf5009fd5ad6a3cebe1 (patch)
treefc1d2f47309c0345a850933496baa40d94bfdcbb /docs/guides/binary/arraybuffer-to-array.md
parent6bfee02301a2e2a0b79339974af0445eb5a2688f (diff)
downloadbun-4c89c60867591b50e0b31bf5009fd5ad6a3cebe1.tar.gz
bun-4c89c60867591b50e0b31bf5009fd5ad6a3cebe1.tar.zst
bun-4c89c60867591b50e0b31bf5009fd5ad6a3cebe1.zip
Add files (#3826)
Diffstat (limited to 'docs/guides/binary/arraybuffer-to-array.md')
-rw-r--r--docs/guides/binary/arraybuffer-to-array.md27
1 files changed, 27 insertions, 0 deletions
diff --git a/docs/guides/binary/arraybuffer-to-array.md b/docs/guides/binary/arraybuffer-to-array.md
new file mode 100644
index 000000000..688030b0a
--- /dev/null
+++ b/docs/guides/binary/arraybuffer-to-array.md
@@ -0,0 +1,27 @@
+---
+name: Convert an ArrayBuffer to an array of numbers
+---
+
+To retrieve the contents of an `ArrayBuffer` as an array of numbers, create a [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) over of the buffer. and use the [`Array.from()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) method to convert it to an array.
+
+```ts
+const buf = new ArrayBuffer(64);
+const arr = new Uint8Array(buf);
+arr.length; // 64
+arr[0]; // 0 (instantiated with all zeros)
+```
+
+---
+
+The `Uint8Array` class supports array indexing and iteration. However if you wish to convert the instance to a regular `Array`, use `Array.from()`. (This will likely be slower than using the `Uint8Array` directly.)
+
+```ts
+const buf = new ArrayBuffer(64);
+const uintArr = new Uint8Array(buf);
+const regularArr = Array.from(uintArr);
+// number[]
+```
+
+---
+
+See [Docs > API > Binary Data](/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.