@hackage basesystems1.1.0.0

Implements encoders/decoders for basesystems

basesystems hackage.haskell.orgbuilds.sr.ht status

This project contains code for encoding/decoding numeric basesystems in Haskell. It's implemented in a strategy pattern style where BaseSystem is a type-class which provides the encoder and decoder methods on, for instance, ShortByteString, in Data.BaseSystem:

class BaseSystem a where
  encoder :: a -> ShortByteString -> String
  decoder :: a -> String -> Maybe ShortByteString

The library creates more BaseSystem classes with methods on normal ByteString in Data.BaseSystem.Strict and LazyByteString in Data.BaseSystem.Lazy. The basesystems passed as a to encoder and decoder are defined in Data.BaseSystem.DigitSystem. See the example.

basesystems is a part of the ipfshs project which contains testing via sourcehut builds and a bug ticket tracker.

Coverage

Eventually, This project aims to implement most if not all of the mulitbase specification's basesytems list.

Currently, the following basesystems are supported:

  • base2
  • base10
  • base16(upper/lower)
  • base32(upper/lower) w/pad + nopad
  • base32hex(lower/upper) w/pad + nopad
  • base58btc
  • base64 w/pad + nopad
  • base64url w/pad + nopad

Example

For an example of using basesystems, we can do the following in GHCI:

Set OverloadedStrings so strings can act as Text data and import the needed functions. Then import the basesystem functions and packValue from Data.BaseSystem.BinaryTranscoder to convert a number value directly into bytes.

λ> {-# LANGUAGE OverloadedStrings #-}
λ> import Data.BaseSystem (encoder, decoder)
λ> import Data.BaseSystem.DigitSystem (base2, base10, base16lower, base32lower)
λ> import Data.BaseSystem.BinaryTranscoder (packValue)

This shows how we can take the binary value of 123 and display it in various number systems.

λ> :t encoder
encoder :: BaseSystem a => a -> ShortByteString -> Text
λ> :t decoder
decoder :: BaseSystem a => a -> Text -> Maybe ShortByteString
λ> encoder base2 $ packValue (123 :: Int)
"1111011"
λ> encoder base16lower $ packValue (123 :: Int)
"7b"
λ> encoder base32lower $ packValue (123 :: Int)
"pm======"

We can also use encoders and decoders to translate one numeric representation to another.

λ> encoder base10 <$> decoder base2 "1111011"
Just "123"
λ> encoder base10 <$> decoder base16lower "7b"
Just "123"
λ> encoder base10 <$> decoder base32lower "pm======"
Just "123"

Licensing

The basesystems project and its modules are free software and licensed under the BSD 3-clause license. See LICENSE.txt.

Copyright © 2026 Zoey McBride | zoeymcbride@mailbox.org