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 Vega 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
Uint8ArrayAPI
Common use cases
Static methods
Uint8Array.fromBase64(string)
Creates a new Uint8Array from a base64-encoded string.
base64String: A string containing base64-encoded data
Uint8Array containing the decoded data
Example:
Uint8Array.fromHex(string)
Creates a new Uint8Array from a hexadecimal string.
hexString: A string containing hexadecimal data (must have an even length and contain only valid hex characters)
Uint8Array containing the decoded data
Example:
Instance methods
Uint8Array.prototype.toBase64()
Converts the Uint8Array to a base64-encoded string.
Uint8Array.prototype.toHex()
Converts the Uint8Array to a hexadecimal string.
Uint8Array.prototype.setFromBase64(string)
Populates the Uint8Array with data from a base64-encoded string.
base64String: A string containing base64-encoded data
read: Number of characters read from the input stringwritten: Number of bytes written to theUint8Array
Uint8Array.prototype.setFromHex(string)
Populates the Uint8Array with data from a hexadecimal string.
hexString: A string containing hexadecimal data (must have an even length and contain only valid hex characters)
read: Number of characters read from the input stringwritten: Number of bytes written to theUint8Array
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
MDN Web Docs
