Deno logoDenoby example

Beginner

cli

Manipulating byte arrays

When working with lower-level data we often deal with byte arrays in the form of Uint8Arrays. There are some common manipulations and queries that can be done and are included with the standard library.

Let's initialize some byte arrays
const a = new Uint8Array([0, 1, 2, 3, 4]);
const b = new Uint8Array([5, 6, 7, 8, 9]);
const c = new Uint8Array([4, 5]);
We can concatenate two byte arrays using the concat method
import { concat } from "https://deno.land/std@0.175.0/bytes/concat.ts";
const d = concat(a, b);
console.log(d); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Sometimes we need to repeat certain bytes
import { repeat } from "https://deno.land/std@0.175.0/bytes/repeat.ts";
console.log(repeat(c, 4)); // [4, 5, 4, 5, 4, 5, 4, 5]
Sometimes we need to mutate a Uint8Array and need a copy
import { copy } from "https://deno.land/std@0.175.0/bytes/copy.ts";
const cpy = new Uint8Array(5);
console.log("Bytes copied:", copy(b, cpy)); // 5
console.log("Bytes:", cpy); // [5, 6, 7, 8, 9]

Run this example locally using the Deno CLI:

deno run https://byexample-wwv03xf36j0g.deno.dev/byte-manipulation.ts

Additional resources: