> ## Documentation Index
> Fetch the complete documentation index at: https://cosmos-docs-cosmjs-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# @cosmjs/encoding

> Encoding utilities for hex, base64, bech32, UTF-8, and more

Cross-platform encoding and decoding utilities that work in both browsers and Node.js without relying on the Node.js `Buffer` class.

```bash theme={"system"}
npm install @cosmjs/encoding
```

## Hex

| Function  | Parameters          | Returns      |
| --------- | ------------------- | ------------ |
| `toHex`   | `data: Uint8Array`  | `string`     |
| `fromHex` | `hexstring: string` | `Uint8Array` |

```typescript theme={"system"}
import { toHex, fromHex } from "@cosmjs/encoding";

const hex = toHex(new Uint8Array([255, 0, 128])); // "ff0080"
const bytes = fromHex("ff0080"); // Uint8Array [255, 0, 128]
```

## Base64

| Function     | Parameters             | Returns      |
| ------------ | ---------------------- | ------------ |
| `toBase64`   | `data: Uint8Array`     | `string`     |
| `fromBase64` | `base64String: string` | `Uint8Array` |

```typescript theme={"system"}
import { toBase64, fromBase64 } from "@cosmjs/encoding";

const b64 = toBase64(new Uint8Array([1, 2, 3])); // "AQID"
const bytes = fromBase64("AQID"); // Uint8Array [1, 2, 3]
```

## Bech32

| Function          | Parameters                                               | Returns                                                  |
| ----------------- | -------------------------------------------------------- | -------------------------------------------------------- |
| `toBech32`        | `prefix: string`, `data: Uint8Array`, `limit?: number`   | `string`                                                 |
| `fromBech32`      | `address: string`, `limit?: number` (default `Infinity`) | `{ readonly prefix: string; readonly data: Uint8Array }` |
| `normalizeBech32` | `address: string`                                        | `string`                                                 |

```typescript theme={"system"}
import { toBech32, fromBech32 } from "@cosmjs/encoding";

const address = toBech32("cosmos", addressBytes);
const { prefix, data } = fromBech32("cosmos1abc...");
```

## UTF-8

| Function   | Parameters                                              | Returns      |
| ---------- | ------------------------------------------------------- | ------------ |
| `toUtf8`   | `str: string`                                           | `Uint8Array` |
| `fromUtf8` | `data: Uint8Array`, `lossy?: boolean` (default `false`) | `string`     |

`fromUtf8` throws on invalid UTF-8 sequences by default. Pass `lossy: true` to replace invalid bytes with the Unicode replacement character (`U+FFFD`).

```typescript theme={"system"}
import { toUtf8, fromUtf8 } from "@cosmjs/encoding";

const bytes = toUtf8("hello world");
const str = fromUtf8(bytes); // "hello world"
```

## ASCII

| Function    | Parameters         | Returns      |
| ----------- | ------------------ | ------------ |
| `toAscii`   | `input: string`    | `Uint8Array` |
| `fromAscii` | `data: Uint8Array` | `string`     |

Throws if the input contains characters outside the printable ASCII range (code points below 0x20 or above 0x7E).

## RFC 3339 Timestamps

| Function      | Parameters                   | Returns  |
| ------------- | ---------------------------- | -------- |
| `toRfc3339`   | `date: Date \| ReadonlyDate` | `string` |
| `fromRfc3339` | `str: string`                | `Date`   |

```typescript theme={"system"}
import { toRfc3339, fromRfc3339 } from "@cosmjs/encoding";

const timestamp = toRfc3339(new Date()); // "2024-01-15T10:30:00.000Z"
const date = fromRfc3339("2024-01-15T10:30:00.000Z");
```

## Uint8Array Utilities

| Function        | Parameters           | Returns      |
| --------------- | -------------------- | ------------ |
| `fixUint8Array` | `source: Uint8Array` | `Uint8Array` |

Ensures a value is a proper `Uint8Array` instance with a direct `ArrayBuffer` backing, which is necessary in some cross-environment scenarios (e.g. when `Uint8Array` subclass instances from protobuf libraries need normalization).
