Uint8Array Base64 and Hex Conversion Utilities
The Uint8Array object represents an array of 8-bit unsigned integers. React Native for Vega includes the standard JavaScript Uint8Array implementation along with additional utility methods for working with binary data.
Available Methods
React Native for Vega's Uint8Array implementation includes the following methods for working with binary data:
Static Methods
Uint8Array.fromBase64(string): Creates a newUint8Arrayfrom a base64-encoded stringUint8Array.fromHex(string): Creates a newUint8Arrayfrom a hexadecimal string
Instance Methods
Uint8Array.prototype.toBase64(): Converts theUint8Arrayto a base64-encoded stringUint8Array.prototype.toHex(): Converts theUint8Arrayto a hexadecimal stringUint8Array.prototype.setFromBase64(string): Populates theUint8Arraywith data from a base64-encoded stringUint8Array.prototype.setFromHex(string): Populates theUint8Arraywith data from a hexadecimal string
These methods provide efficient ways to convert between binary data and string representations.
Static Methods
Uint8Array.fromBase64(string)
static fromBase64(base64String: string): Uint8Array
Creates a new Uint8Array from a base64-encoded string.
Parameters:
- base64String: A string containing base64-encoded data.
Returns:
A new Uint8Array containing the decoded data.
Notes:
- Only the default implementation is supported - there is no support for options such as base64url alphabet or lastChunkHandling. The standard base64 alphabet and loose lastChunkHandling are currently the only supported modes of operation.
Example:
const data = Uint8Array.fromBase64('SGVsbG8gV29ybGQ=');
console.log(data); // Uint8Array representing "Hello World"
Uint8Array.fromHex(string)
static fromHex(hexString: string): Uint8Array
Creates a new Uint8Array from a hexadecimal string.
Parameters:
- hexString: A string containing hexadecimal data. Must have an even length and contain only valid hexadecimal characters (0-9, A-F, a-f).
Returns:
A new Uint8Array containing the decoded data.
Example:
const data = Uint8Array.fromHex('48656c6c6f20576f726c64');
console.log(data); // Uint8Array representing "Hello World"
Instance Methods
Uint8Array.prototype.toBase64()
toBase64(): string
Converts the Uint8Array to a base64-encoded string.
Returns:
A base64-encoded string representing the binary data.
Notes:
- Only the default implementation is supported - there is no support for options such as base64url alphabet or omitPadding. The standard base64 alphabet is used and padding is always included (equivalent to omitPadding: false).
Example:
const array = new Uint8Array([
72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100
]);
const base64 = array.toBase64();
console.log(base64); // "SGVsbG8gV29ybGQ="
Uint8Array.prototype.toHex()
toHex(): string
Converts the Uint8Array to a hexadecimal string.
Returns:
A hexadecimal string representing the binary data.
Example:
const array = new Uint8Array([
72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100
]);
const hex = array.toHex();
console.log(hex); // "48656c6c6f20576f726c64"
Uint8Array.prototype.setFromBase64(string)
setFromBase64(base64String: string): {read: number, written: number}
Populates the Uint8Array with data from a base64-encoded string.
Parameters:
- base64String: A string containing base64-encoded data.
Returns:
An object with:
- read: Number of characters read from the input string
- written: Number of bytes written to the
Uint8Array
Notes:
- Only the default implementation is supported - there is no support for options such as base64url alphabet or lastChunkHandling. The standard base64 alphabet and loose lastChunkHandling are currently the only supported modes of operation.
Example:
const array = new Uint8Array(20);
const result = array.setFromBase64('SGVsbG8gV29ybGQ=');
console.log(result); // { read: 16, written: 11 }
console.log(array); // Uint8Array containing "Hello World" followed by zeros
Uint8Array.prototype.setFromHex(string)
setFromHex(hexString: string): {read: number, written: number}
Populates the Uint8Array with data from a hexadecimal string.
Parameters:
- hexString: A string containing hexadecimal data. Must have an even length and contain only valid hexadecimal characters (0-9, A-F, a-f).
Returns:
An object with:
- read: Number of characters read from the input string
- written: Number of bytes written to the
Uint8Array
Example:
const array = new Uint8Array(20);
const result = array.setFromHex('48656c6c6f20576f726c64');
console.log(result); // { read: 22, written: 11 }
console.log(array); // Uint8Array containing "Hello World" followed by zeros
Notes
- These methods are optimized for performance and use native implementations where available.
- When working with large binary data, these methods are significantly more efficient than manual conversion approaches.
- These methods are compatible with the standard JavaScript
Uint8ArrayAPI and don't interfere with existing functionality.
Last updated: May 13, 2026

