Hex and base64 encoding

There are a few cases where it would be practical to encode and decode between different string and array buffer formats. The Deno Standard Library makes this easy.

import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
import { decodeHex, encodeHex } from "jsr:@std/encoding/hex";
const base64Encoded = encodeBase64("somestringtoencode");
console.log(encodeBase64(new Uint8Array([1, 32, 67, 120, 19])));
const base64Decoded = decodeBase64(base64Encoded);
const textDecoder = new TextDecoder();
console.log(textDecoder.decode(base64Decoded));
const hexEncoded = encodeHex("somestringtoencode");
console.log(hexEncoded);
const hexDecoded = decodeHex(hexEncoded);
console.log(textDecoder.decode(hexDecoded));

Run this example locally using the Deno CLI:

deno run https://docs.deno.com/examples/scripts/hex_base64_encoding.ts

Did you find what you needed?