aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/binary/arraybuffer-to-array.md
blob: 688030b0a0d15d05bc1897aefdbeed21fc4dc5f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.
iro/queue-response-experiment&id=8275b8ccd2f0d39b482950dd809d6753c165607c&follow=1'>[types] fix `blob.json()` (#3995)Gravatar Alex Lam S.L 1-4/+4 2023-08-04FFI typo (#3973)Gravatar dave caruso 3-5/+10 2023-08-04Buffer.copy should ignore out-of-range sourceEnd (#3971)Gravatar Yifei Wang 2-17/+34 2023-08-04[install] store resolved workspace path in lockfile (#3974)Gravatar Alex Lam S.L 5-82/+187 2023-08-04Fix types (#3963)Gravatar Colin McDonnell 14-274/+892 2023-08-04feat(hot-clear-screen): clear terminal on hot reload (#3976)Gravatar simylein 1-0/+7