Codec.Binary.Base16

sandi-0.4.1: Data encoding library

Copyright | (c) 2012 Magnus Therning
License | BSD3
Safe Haskell | None
Language | Haskell98

Codec.Binary.Base16

Description

Implemention of base 16 encoding (hex encoding) as specified in RFC 4648 (http://tools.ietf.org/html/rfc4648).

Synopsis

- b16Enc :: ByteString -> ByteString
- b16Dec :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString)
- encode :: ByteString -> ByteString
- decode :: ByteString -> Either (ByteString, ByteString) ByteString

# Documentation

b16Enc #

Arguments

:: ByteString |
-> ByteString | The encoded string

Encoding function.

This function, unlike some other encoding functions in the library, simply cannot fail. Double the length of the input string is allocated for the encoded data, which is guaranteed to hold the result.

    >>> b16Enc $ Data.ByteString.pack [0x00]
    "00"

    >>> b16Enc $ Data.ByteString.Char8.pack "foobar"
    "666F6F626172"

b16Dec :: ByteString -> Either (ByteString, ByteString) (ByteString, ByteString) #

Decoding function.

The returned value on success is `Right (<decoded part>, <undecoded part>)` (the undecoded part is either a empty or a single byte), and on failure it's `Left (<decoded part>, <undecodable part>)`. Space equal to the length of the input string is allocated, which is more than enough to hold the decoded data.

    >>> b16Dec $ Data.ByteString.Char8.pack "00"
    Right ("\NUL","")

    >>> b16Dec $ Data.ByteString.Char8.pack "666F6F626172"
    Right ("foobar","")

    >>> b16Dec $ Data.ByteString.Char8.pack "666F6F62617"
    Right ("fooba","7")
    >>> b16Dec $ Data.ByteString.Char8.pack "666F6F62617g"
    Left ("fooba","g")

encode :: ByteString -> ByteString #

A synonym for `b16_enc`.

decode :: ByteString -> Either (ByteString, ByteString) ByteString #

A synonum for `b16_dec`.