Skip to content

Base64

Utility functions for working with RFC-4648 Base64.

Examples

Encoding to Base64

Values can be encoded to Base64 with:

import { Base64 } from 'ox'
 
const value_string = Base64.fromString('Hello World!')
'SGVsbG8gV29ybGQh=='
const value_bytes = Base64.fromBytes(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
'SGVsbG8gV29ybGQh=='
const value_hex = Base64.fromHex('0x48656c6c6f20576f726c6421')
'SGVsbG8gV29ybGQh=='

Decoding Base64

Values can be decoded from Base64 with:

import { Base64 } from 'ox'
 
const value_string = Base64.toString('SGVsbG8gV29ybGQh==')
'Hello World!'
const value_bytes = Base64.toBytes('SGVsbG8gV29ybGQh==')
Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
const value_hex = Base64.toHex('SGVsbG8gV29ybGQh==')
'0x48656c6c6f20576f726c6421'

Functions

NameDescription
Base64.fromBytesEncodes a Bytes.Bytes to a Base64-encoded string (with optional padding and/or URL-safe characters).
Base64.fromHexEncodes a Hex.Hex to a Base64-encoded string (with optional padding and/or URL-safe characters).
Base64.fromStringEncodes a string to a Base64-encoded string (with optional padding and/or URL-safe characters).
Base64.toBytesDecodes a Base64-encoded string (with optional padding and/or URL-safe characters) to Bytes.Bytes.
Base64.toHexDecodes a Base64-encoded string (with optional padding and/or URL-safe characters) to Hex.Hex.
Base64.toStringDecodes a Base64-encoded string (with optional padding and/or URL-safe characters) to a string.