HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulememory-0.18.0Haskell2010

Data.ByteArray.Encoding

Base conversions for ByteArray.

  • 1 type
  • 2 values
  • Packagememory-0.18.0
  • Exports3
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceEncoding.hs
valueconvertToBase
  1. :: (ByteArrayAccess bin, ByteArray bout)
  2. => Base
  3. -> bin
  4. -> bout
#

Encode some bytes to the equivalent representation in a specific Base.

Examples

Convert a ByteString to base-64:

Example1 expression
convertToBase Base64 ("foobar" :: ByteString) :: ByteString"Zm9vYmFy"
valueconvertFromBase
  1. :: (ByteArrayAccess bin, ByteArray bout)
  2. => Base
  3. -> bin
  4. -> Either String bout
#

Try to decode some bytes from the equivalent representation in a specific Base.

Examples

Successfully convert from base-64 to a ByteString:

Example1 expression
convertFromBase Base64 ("Zm9vYmFy" :: ByteString) :: Either String ByteStringRight "foobar"

Trying to decode invalid data will return an error string:

Example1 expression
convertFromBase Base64 ("!!!" :: ByteString) :: Either String ByteStringLeft "base64: input: invalid length"
datadata Base
#

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.

Examples

A quick example to show the differences:

Example7 expressions
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"

Constructors

Instances2Eq, Show
  • Eq BaseDefined in memory-0.18.0 · Data.ByteArray.Encoding
  • Show BaseDefined in memory-0.18.0 · Data.ByteArray.Encoding