Skip to content

Primitive Values

There are a few primitives to choose from:

  • bool is an 8-bit value representing either true or false.
    • Encoded as 1 if true, and as 0 if false.
  • byte is an 8-bit value representing an unsigned number between 0 and 255.
    • Encoded as-is
  • i32 is a 32-bit signed integer number container.
    • Encoded as-is
  • f32 is a 32-bit signed floating-point number container.
    • Encoded as-is
  • string is a variable-length string of ASCII characters.
    • A string of characters followed by a \0 terminal character.
import bin from 'typed-binary';
const buffer = Buffer.alloc(16);
// Writing four bytes into the buffer
const writer = new bin.BufferWriter(buffer);
bin.byte.write(writer, 'W'.charCodeAt(0));
bin.byte.write(writer, 'o'.charCodeAt(0));
bin.byte.write(writer, 'w'.charCodeAt(0));
bin.byte.write(writer, 0);
const reader = new bin.BufferReader(buffer);
console.log(bin.string.read(reader)); // > Wow