Uint8Array Global Method Support
React Native for Kepler provides native implementations of Uint8Array static and instance methods as listed in MDN web docs
to support efficient binary data encoding and decoding.
Working with binary data is a common requirement in many applications. React Native for Kepler provides optimized methods for converting between binary data (Uint8Array) and string representations (base64 and hexadecimal).
These methods are added to the global Uint8Array
object and are available throughout your application without requiring any imports.
Key Features
- Efficient binary-to-string conversions: Convert binary data to and from base64 and hexadecimal strings
- Native implementation: Methods are optimized for performance using native code
- Standard compatibility: Fully compatible with the standard JavaScript
Uint8Array
API
Common use cases
Static Methods
Uint8Array.fromBase64(string)
Creates a new Uint8Array
from a base64-encoded string.
static fromBase64(base64String: string): Uint8Array
Parameters:
base64String
: A string containing base64-encoded data
Returns:
A new Uint8Array
containing the decoded data
Example:
const data = Uint8Array.fromBase64('SGVsbG8gV29ybGQ=');
console.log(data); // Uint8Array representing "Hello World"
Uint8Array.fromHex(string)
Creates a new Uint8Array
from a hexadecimal string.
static fromHex(hexString: string): Uint8Array
Parameters:
hexString
: A string containing hexadecimal data (must have an even length and contain only valid hex characters)
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()
Converts the Uint8Array
to a base64-encoded string.
toBase64(): string
Returns:
A base64-encoded string representing the binary data
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()
Converts the Uint8Array
to a hexadecimal string.
toHex(): 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)
Populates the Uint8Array
with data from a base64-encoded string.
setFromBase64(base64String: string): {read: number, written: number}
Parameters:
base64String
: A string containing base64-encoded data
Returns:
An object with:
read
: Number of characters read from the input stringwritten
: Number of bytes written to theUint8Array
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)
Populates the Uint8Array
with data from a hexadecimal string.
setFromHex(hexString: string): {read: number, written: number}
Parameters:
hexString
: A string containing hexadecimal data (must have an even length and contain only valid hex characters)
Returns:
An object with:
read
: Number of characters read from the input stringwritten
: Number of bytes written to theUint8Array
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
Troubleshooting
- Only the standard base64 alphabet is supported (no base64url variant).
- Hexadecimal strings must have an even length and contain only valid hex characters (0-9, A-F, a-f).
Additional Resources
Last updated: Sep 30, 2025