Open in a new tab

Turn a text into its UTF-8 bytes and show them as Base64 and hex; decode a pasted Base64 string back to text.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

You need a transport-safe string for binary or non-ASCII data (a data URL, a JSON field, a header), or you receive one and must check it before decoding.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.

What it shows

  • Import only the module descriptors you use, then registerAllDeep.
  • utf8.toBytes / utf8.fromBytes: text and its bytes, accents and symbols included (ö is 2 bytes, ✓ is 3).
  • b64.fromBytes and hex.fromBytes: two text views of the same bytes.
  • b64.test before b64.toBytes: input that is not Base64 gets a message, not garbage.

Code

main.js

javascriptApache-2.054 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { utf8 } from '@awacloud/fw/io/codec/utf8.js';
import { b64 } from '@awacloud/fw/io/codec/b64.js';
import { hex } from '@awacloud/fw/io/codec/hex.js';

// Register only what this page uses: registerAllDeep walks each module's deps.
fw.runtime.registerAllDeep([utf8, b64, hex]);
const UTF8 = fw.runtime.resolve('utf8');
const BASE64 = fw.runtime.resolve('b64');
const HEX = fw.runtime.resolve('hex');

/**
 * Text → its UTF-8 bytes, shown three ways.
 * @param {string} value
 */
export function encode(value) {
    const bytes = UTF8.toBytes(value);
    return { bytes: bytes.length, base64: BASE64.fromBytes(bytes), hex: HEX.fromBytes(bytes) };
}

/**
 * Base64 → text, or the reason it is not Base64.
 * @param {string} encoded
 * @returns {{ text: string } | { error: string }}
 */
export function decodeBase64(encoded) {
    if (!BASE64.test(encoded)) {
        return { error: 'Not Base64: use A–Z, a–z, 0–9, + and /, padded with = to a multiple of 4 characters.' };
    }
    return { text: UTF8.fromBytes(BASE64.toBytes(encoded)) };
}

/** @param {string} id */
const field = (id) => /** @type {HTMLInputElement} */ (document.getElementById(id));
/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLOutputElement} */ (document.getElementById(id)).value = value; };

function showEncoded() {
    const { bytes, base64, hex: hexText } = encode(field('input').value);
    show('bytes', `${bytes} bytes`);
    show('base64', base64);
    show('hex', hexText);
}

function showDecoded() {
    const result = decodeBase64(field('pasted').value.trim());
    show('decoded', 'text' in result ? result.text : result.error);
}

field('input').addEventListener('input', showEncoded);
field('pasted').addEventListener('input', showDecoded);
showEncoded();
showDecoded();

index.html

htmlApache-2.032 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Base64 and UTF-8 codecs</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Text, bytes, Base64 and hex</h1>
    <h2>Encode</h2>
    <label for="input">Text</label>
    <textarea id="input">Hello, wörld ✓</textarea>
    <dl>
      <dt>UTF-8</dt><dd><output id="bytes" for="input"></output></dd>
      <dt>Base64</dt><dd><output id="base64" for="input"></output></dd>
      <dt>Hex</dt><dd><output id="hex" for="input"></output></dd>
    </dl>
    <h2>Decode</h2>
    <label for="pasted">Base64</label>
    <input id="pasted" value="w6kgbGEgY2FydGU=" spellcheck="false">
    <p>Text: <output id="decoded" for="pasted"></output></p>
  </main>
</body>
</html>