Dev Helpers
← Back

Build a UTF-8 length-prefixed record

Build a UTF-8 length-prefixed record using JavaScript.

Open this example in the interactive library

Source code

const bytes = new TextEncoder().encode("press");
const record = new Uint8Array(2 + bytes.length);
new DataView(record.buffer).setUint16(0, bytes.length);
record.set(bytes, 2);
console.log([...record]);

Sample output

[
    0,   5, 112, 114,
  101, 115, 115
]