Creates a new ByteString from a Uint8Array or array of bytes.
The byte data
Debug label: Object.prototype.toString reports [object ByteString].
The underlying byte data (LIVE reference - mutations are visible to the
ByteString; use toUint8Array() for a copy).
The length of the byte string in bytes (typed-array vocabulary).
StaticfromCreates a new ByteString from various input types.
Uint8Array, number array, or string
New ByteString instance
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.bytes, new Uint8Array([1, 2, 3, 4])); // original unchanged
Iterate the bytes.
StaticfromCreate a ByteString from a hex string. Inherits hexToBytes's
validation: whitespace-tolerant, but throws CborError Custom on odd
length or non-hex characters.
Render the bytes as lowercase hex.
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.bytes, 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