Skip to content
heff

std.hex

Hexadecimal (RFC 4648’s base16): bytes written as text, each as two digits of 16, 0-9 and a-f, the high four bits first, as Go’s encoding/hex writes them. encode writes the letters in lower case; decode reads either case, and gives Nothing where the text is an odd number of digits long or has anything but digits. Import it by a name, import std.hex as Hex.

import bytes as Bytes
import std.hex as Hex
-- | >>> dump
-- | "4869ff"
dump : String
dump = Hex.encode b["Hi", 255]

encode

encode : Bytes -> String

The bytes, two digits each, the letters in lower case. RFC 4648’s examples:

>>> List.map (\s -> encode (Bytes.fromString s)) ["", "f", "fo", "foo", "foob", "fooba", "foobar"]
["", "66", "666f", "666f6f", "666f6f62", "666f6f6261", "666f6f626172"]
>>> encode b[0, 1, 15, 16, 127, 128, 254, 255]
"00010f107f80feff"

decode

decode : String -> Maybe Bytes

The bytes the digits are, their letters in either case: Nothing where there is an odd number of them, or anything but digits.

>>> List.map decode ["", "66", "666F6f", "666f6f626172"]
[Just b[], Just b["f"], Just b["foo"], Just b["foobar"]]
>>> decode "00010F107f80FEff"
Just b[0, 1, 15, 16, 127, 128, 254, 255]

(an odd number of digits, what is not a digit, white space)

>>> List.map decode ["6", "666", "6g", "zz", "66 6f", "66\n", "0x66"]
[Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing]