---
title: "@std/encoding"
description: "Utilities for encoding and decoding common formats like hex, base64, and varint"
jsr: jsr:@std/encoding
pkg: encoding
version: 1.0.10
generated: true
stability: stable
---
<!-- Autogenerated from JSR docs. Do not edit directly. -->

## Overview

<p>Utilities for encoding and decoding common formats like hex, base64, and varint.</p>
<h2 id="basic-usage">
Basic Usage</h2>

```js
import { encodeBase64, decodeBase64 } from "@std/encoding";
import { assertEquals } from "@std/assert";

const foobar = new TextEncoder().encode("foobar");
assertEquals(encodeBase64(foobar), "Zm9vYmFy");
assertEquals(decodeBase64("Zm9vYmFy"), foobar);
```

<h2 id="various-encoding-formats">
Various Encoding Formats</h2>

```js
import {
  encodeHex,
  encodeBase32,
  encodeBase58,
  encodeBase64,
  encodeAscii85,
  decodeHex,
  decodeBase32,
  decodeBase58,
  decodeBase64,
  decodeAscii85,
} from "@std/encoding";
import { assertEquals } from "@std/assert";

// Many different encodings for different character sets
assertEquals(encodeHex("Hello world!"), "48656c6c6f20776f726c6421");
assertEquals(encodeBase32("Hello world!"), "JBSWY3DPEB3W64TMMQQQ====");
assertEquals(encodeBase58("Hello world!"), "2NEpo7TZRhna7vSvL");
assertEquals(encodeBase64("Hello world!"), "SGVsbG8gd29ybGQh");
assertEquals(encodeAscii85("Hello world!"), "87cURD]j7BEbo80");

// Decoding
assertEquals(new TextDecoder().decode(decodeHex("48656c6c6f20776f726c6421")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase32("JBSWY3DPEB3W64TMMQQQ====")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase58("2NEpo7TZRhna7vSvL")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeBase64("SGVsbG8gd29ybGQh")), "Hello world!");
assertEquals(new TextDecoder().decode(decodeAscii85("87cURD]j7BEbo80")), "Hello world!");
```

<h2 id="url-safe-base64">
URL-Safe Base64</h2>

```js
import { encodeBase64, encodeBase64Url } from "@std/encoding";
import { assertEquals } from "@std/assert";

assertEquals(encodeBase64("ice creams"), "aWNlIGNyZWFtcw=="); // Not url-safe because of `=`
assertEquals(encodeBase64Url("ice creams"), "aWNlIGNyZWFtcw"); // URL-safe!

// Base64Url replaces + with - and / with _
assertEquals(encodeBase64("subjects?"), "c3ViamVjdHM/"); // slash is not URL-safe
assertEquals(encodeBase64Url("subjects?"), "c3ViamVjdHM_"); // _ is URL-safe
```

<h2 id="binary-data-encoding">
Binary Data Encoding</h2>

```js
import { encodeHex, encodeBase64 } from "@std/encoding";
import { assertEquals } from "@std/assert";

// Working with binary data
const binaryData = new Uint8Array([0xDE, 0xAD, 0xBE, 0xEF]);
assertEquals(encodeHex(binaryData), "deadbeef");
assertEquals(encodeBase64(binaryData), "3q2+7w==");
```

<h2 id="varint-encoding">
Varint Encoding</h2>
<p>Learn more from the <a href="https://protobuf.dev/programming-guides/encoding/#varints" rel="nofollow">protobuf Varint encoding docs</a>.</p>

```js
import { encodeVarint, decodeVarint } from "@std/encoding";
import { assertEquals } from "@std/assert";

// Varint encoding support
assertEquals(encodeVarint(9601n), [new Uint8Array([129, 75]), 2]);

// Decode a varint
const bytes = new Uint8Array([129, 75]);
assertEquals(decodeVarint(bytes), [9601n, 2]);
```

### Add to your project

```sh
deno add jsr:@std/encoding
```

<a href="https://jsr.io/@std/encoding/doc" class="docs-cta jsr-cta">See all symbols in @std/encoding on
<svg class="inline ml-1" viewBox="0 0 13 7" aria-hidden="true" height="20"><path d="M0,2h2v-2h7v1h4v4h-2v2h-7v-1h-4" fill="#083344"></path><g fill="#f7df1e"><path d="M1,3h1v1h1v-3h1v4h-3"></path><path d="M5,1h3v1h-2v1h2v3h-3v-1h2v-1h-2"></path><path d="M9,2h3v2h-1v-1h-1v3h-1"></path></g></svg></a>

<!-- custom:start -->
<!-- Add persistent custom content below. This section is preserved across generations. -->

<!-- custom:end -->
