Available

amalgame-encoding

Base64, Hex, percent-encoding.

amc package add encoding GitHub ↗ ← Back to the ecosystem

Overview

Three static classes: Base64 (RFC 4648), Hex and Url (percent-encoding, RFC 3986). Bytes are a List<int>, one value from 0 to 255 per item.

Features

  • ✓Base64.Encode / Decode, and the URL-safe EncodeUrl / DecodeUrl: encoding adds = padding, decoding works with or without it
  • ✓Hex.Encode (lowercase), Hex.EncodeUpper, Hex.Decode (either case)
  • ✓Url.EncodeComponent for one part of a URL, Url.Encode which keeps the separators (/ ? # & =…), Url.Decode
  • ✓Base64.IsValid and Hex.IsValid to check input
  • ✓Pure Amalgame, no C dependency

Limits

  • –Base64 and Hex take bytes, not a string.
  • –Url.Decode does not turn + into a space: it is not an HTML form decoder.

Example

After amc package add encoding, put this code in main.am and run amc run main.am.

import Amalgame.Collections
import Amalgame.Encoding

public class Program {
    public static void Main() {
        let bytes: List<int> = Hex.Decode("deadbeef")    // 4 bytes
        Console.WriteLine(Base64.Encode(bytes))          // 3q2+7w==
        Console.WriteLine(Base64.EncodeUrl(bytes))       // 3q2-7w==
        Console.WriteLine(Hex.EncodeUpper(bytes))        // DEADBEEF

        let back: List<int> = Base64.Decode("3q2+7w")    // no padding: accepted
        Console.WriteLine(Hex.Encode(back))              // deadbeef

        Console.WriteLine(Url.EncodeComponent("a b&c"))  // a%20b%26c
        Console.WriteLine(Url.Decode("a%20b%26c"))       // a b&c
    }
}

At a glance

Related packages