BC-DCBOR TypeScript Library - v1.0.0-alpha.13
    Preparing search index...

    Class ByteString

    Represents a CBOR byte string (major type 2).

    Use Cases:

    • Binary data such as images, audio, or other non-text content
    • Cryptographic values like hashes, signatures, and public keys
    • Embedded CBOR (wrapped with tag 24)
    • Other serialized data formats embedded in CBOR
    // Creating a byte string from various sources
    const bytes1 = new ByteString(new Uint8Array([1, 2, 3, 4]));
    const bytes2 = ByteString.from([5, 6, 7, 8]);
    const bytes3 = ByteString.from(new Uint8Array([9, 10, 11, 12]));

    // Converting to and from CBOR
    const cborValue = bytes1.toCbor();

    // ByteString provides Uint8Array-like operations
    const bytes = new ByteString(new Uint8Array([1, 2]));
    bytes.extend(new Uint8Array([3, 4]));
    assert(bytes.len() === 4);
    assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));
    Index

    Constructors

    • Creates a new ByteString from a Uint8Array or array of bytes.

      Parameters

      • data: Uint8Array<ArrayBufferLike> | number[]

        The byte data

      Returns ByteString

      // From a Uint8Array
      const bytes1 = new ByteString(new Uint8Array([1, 2, 3, 4]));

      // From a number array
      const bytes2 = new ByteString(new Uint8Array([5, 6, 7, 8]));

    Methods

    • Creates a new ByteString from various input types.

      Parameters

      • data: string | Uint8Array<ArrayBufferLike> | number[]

        Uint8Array, number array, or string

      Returns ByteString

      New ByteString instance

      const bytes1 = ByteString.from([1, 2, 3, 4]);
      const bytes2 = ByteString.from(new Uint8Array([5, 6, 7, 8]));
      const bytes3 = ByteString.from("hello");
    • Returns a reference to the underlying byte data.

      Returns Uint8Array

      The raw bytes

      const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
      assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));

      // You can use standard slice operations on the result
      assert.deepEqual(bytes.data().slice(1, 3), new Uint8Array([2, 3]));
    • Returns the length of the byte string in bytes.

      Returns number

      Number of bytes

      const empty = new ByteString(new Uint8Array([]));
      assert(empty.len() === 0);

      const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
      assert(bytes.len() === 4);
    • Returns true if the byte string contains no bytes.

      Returns boolean

      true if empty

      const empty = new ByteString(new Uint8Array([]));
      assert(empty.isEmpty());

      const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
      assert(!bytes.isEmpty());
    • Extends the byte string with additional bytes.

      Parameters

      • other: Uint8Array<ArrayBufferLike> | number[]

        Bytes to append

      Returns void

      const bytes = new ByteString(new Uint8Array([1, 2]));
      bytes.extend(new Uint8Array([3, 4]));
      assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));

      // You can extend with different types
      bytes.extend([5, 6]);
      assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4, 5, 6]));
    • Creates a new Uint8Array containing a copy of the byte string's data.

      Returns Uint8Array

      Copy of the data

      const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
      const arr = bytes.toUint8Array();
      assert.deepEqual(arr, new Uint8Array([1, 2, 3, 4]));

      // The returned array is a clone, so you can modify it independently
      const arr2 = bytes.toUint8Array();
      arr2[0] = 99;
      assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4])); // original unchanged
    • Returns an iterator over the bytes in the byte string.

      Returns Iterator<number>

      Iterator yielding each byte

      const bytes = new ByteString(new Uint8Array([1, 2, 3]));
      const iter = bytes.iter();

      assert(iter.next().value === 1);
      assert(iter.next().value === 2);
      assert(iter.next().value === 3);
      assert(iter.next().done);

      // You can also use for loops
      let sum = 0;
      for (const byte of bytes) {
      sum += byte;
      }
      assert(sum === 6);
    • Makes ByteString iterable.

      Returns Iterator<number>

    • Converts the ByteString to a CBOR value.

      Returns Cbor

      CBOR byte string

      const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
      const cborValue = bytes.toCbor();
    • Attempts to convert a CBOR value into a ByteString.

      Parameters

      • cbor: Cbor

        CBOR value

      Returns ByteString

      ByteString if successful

      Error if the CBOR value is not a byte string

      const cborValue = toCbor(new Uint8Array([1, 2, 3, 4]));
      const bytes = ByteString.fromCbor(cborValue);
      assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));

      // Converting from a different CBOR type throws
      const cborInt = toCbor(42);
      try {
      ByteString.fromCbor(cborInt); // throws
      } catch(e) {
      // Error: Wrong type
      }
    • Get element at index.

      Parameters

      • index: number

        Index to access

      Returns number | undefined

      Byte at index or undefined