b

based

@preview

Encoder and decoder for base64, base32, and base16.

v0.2.0
MIT
2 downloads

Package Information

Last Updated
Categories
scripting

1. Get the package

Download the package using the TPIX CLI:

tpix get @preview/based:0.2.0

2. Import in your Typst file

Add this to your .typ file:

#import "@preview/based:0.2.0": *

Version History

0.2.0
37bf8dd31b40...
0.1.0
df4a636daa81...

based

A package for encoding and decoding in base64, base32, and base16.

Usage

The package comes with three submodules: base64, base32, and base16. All of them have an encode and decode function. The package also provides the function aliases

  • encode64 / decode64,
  • encode32 / decode32, and
  • encode16 / decode16.

Both base64 and base32 allow you to choose whether to use padding for encoding via the pad parameter, which is enabled by default. Base64 also allows you to encode with the URL-safe alphabet by enabling the url parameter, while base32 allows you to encode or decode with the "extended hex" alphabet via the hex parameter. Both options are disabled by default. The base16 encoder uses lowercase letters, the decoder is case-insensitive.

You can encode strings, arrays and bytes. The encode function will return a string, while the decode function will return bytes.

Example

#import "@preview/based:0.2.0": base64, base32, base16

#table(
  columns: 3,
  
  table.header[*Base64*][*Base32*][*Base16*],

  raw(base64.encode("Hello world!")),
  raw(base32.encode("Hello world!")),
  raw(base16.encode("Hello world!")),

  str(base64.decode("SGVsbG8gd29ybGQh")),
  str(base32.decode("JBSWY3DPEB3W64TMMQQQ====")),
  str(base16.decode("48656C6C6F20776F726C6421"))
)

Result of example code.