Creates a new ByteString from a Uint8Array or array of bytes.
The byte data
StaticfromCreates a new ByteString from various input types.
Uint8Array, number array, or string
New ByteString instance
Returns a reference to the underlying byte data.
The raw bytes
Extends the byte string with additional bytes.
Bytes to append
Creates a new Uint8Array containing a copy of the byte string's data.
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.
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.
StaticfromAttempts to convert a CBOR value into a ByteString.
CBOR value
ByteString if successful
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.
Index to access
Byte at index or undefined
Equality comparison.
ByteString to compare with
true if equal
Represents a CBOR byte string (major type 2).
Use Cases:
Example