# Base64

Utility functions for working with [RFC-4648](https://datatracker.ietf.org/doc/html/rfc4648) Base64.

## Examples

### Encoding to Base64

Values can be encoded to Base64 with:

* [`Base64.fromString`](/api/Base64/fromString), or

* [`Base64.fromBytes`](/api/Base64/fromBytes), or

* [`Base64.fromHex`](/api/Base64/fromHex)

```ts twoslash
import { Base64 } from 'ox'

const value_string = Base64.fromString('Hello World!')
// @log: 'SGVsbG8gV29ybGQh=='

const value_bytes = Base64.fromBytes(
  new Uint8Array([
    72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33
  ])
)
// @log: 'SGVsbG8gV29ybGQh=='

const value_hex = Base64.fromHex(
  '0x48656c6c6f20576f726c6421'
)
// @log: 'SGVsbG8gV29ybGQh=='
```

### Decoding Base64

Values can be decoded from Base64 with:

* [`Base64.toString`](/api/Base64/toString), or

* [`Base64.toBytes`](/api/Base64/toBytes), or

* [`Base64.toHex`](/api/Base64/toHex)

```ts twoslash
import { Base64 } from 'ox'

const value_string = Base64.toString('SGVsbG8gV29ybGQh==')
// @log: 'Hello World!'

const value_bytes = Base64.toBytes('SGVsbG8gV29ybGQh==')
// @log: Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]

const value_hex = Base64.toHex('SGVsbG8gV29ybGQh==')
// @log: '0x48656c6c6f20576f726c6421'
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Base64.fromBytes`](/api/Base64/fromBytes) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) to a Base64-encoded string (with optional padding and/or URL-safe characters). |
| [`Base64.fromHex`](/api/Base64/fromHex) | Encodes a [`Hex.Hex`](/api/Hex/types#hex) to a Base64-encoded string (with optional padding and/or URL-safe characters). |
| [`Base64.fromString`](/api/Base64/fromString) | Encodes a string to a Base64-encoded string (with optional padding and/or URL-safe characters). |
| [`Base64.toBytes`](/api/Base64/toBytes) | Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to [`Bytes.Bytes`](/api/Bytes/types#bytes). |
| [`Base64.toHex`](/api/Base64/toHex) | Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to [`Hex.Hex`](/api/Hex/types#hex). |
| [`Base64.toString`](/api/Base64/toString) | Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to a string. |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Base64.InvalidCharacterError`](/api/Base64/errors#base64invalidcharactererror) | Thrown when a Base64 string contains an invalid character. |
| [`Base64.InvalidLengthError`](/api/Base64/errors#base64invalidlengtherror) | Thrown when a Base64 string has an impossible length. |
| [`Base64.InvalidPaddingError`](/api/Base64/errors#base64invalidpaddingerror) | Thrown when a Base64 string contains too many trailing `=` padding characters. |
