Encode some bytes to the equivalent representation in a specific Base.
Examples
Convert a ByteString to base-64:
convertToBase Base64 ("foobar" :: ByteString) :: ByteString"Zm9vYmFy"
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27
Encode some bytes to the equivalent representation in a specific Base.
Convert a ByteString to base-64:
convertToBase Base64 ("foobar" :: ByteString) :: ByteString"Zm9vYmFy"
Try to decode some bytes from the equivalent representation in a specific Base.
Successfully convert from base-64 to a ByteString:
convertFromBase Base64 ("Zm9vYmFy" :: ByteString) :: Either String ByteStringRight "foobar"
Trying to decode invalid data will return an error string:
convertFromBase Base64 ("!!!" :: ByteString) :: Either String ByteStringLeft "base64: input: invalid length"
The different bases that can be used.
See RFC4648 for details. In particular, Base64 can be standard or URL-safe. URL-safe encoding is often used in other specifications without padding characters.
RFC 2045 defines a separate Base64 encoding, which is not supported. This format requires a newline at least every 76 encoded characters, which works around limitations of older email programs that could not handle long lines. Be aware that other languages, such as Ruby, encode the RFC 2045 version by default. To decode their output, remove all newlines before decoding.
A quick example to show the differences:
let input = "Is 3 > 2?" :: ByteStringlet convertedTo base = convertToBase base input :: ByteStringconvertedTo Base16"49732033203e20323f"convertedTo Base32"JFZSAMZAHYQDEPY="convertedTo Base64"SXMgMyA+IDI/"convertedTo Base64URLUnpadded"SXMgMyA-IDI_"convertedTo Base64OpenBSD"QVKeKw.8GBG9"
Base16similar to hexadecimal
Base32Base64standard Base64
Base64URLUnpaddedunpadded URL-safe Base64
Base64OpenBSDBase64 as used in OpenBSD password encoding (such as bcrypt)