jxxcarlson/hex/4.0.0

NOTE: the latest version is 4.0.1

Hex.Convert

The Hex.Convert package converts Bytes values to and from String values. Three functions are exposed:

  • toString : Bytes -> String
  • toBytes : String -> Maybe Bytes
  • blocks : Int -> String -> List String

The toString function converts a Bytes value to a string of hexadecimal characters representing the Bytes value. The toBytes function converts a string to a value of type Maybe Bytes. This choice of return type is necessary to account for the fact that a function call can fail, e.g., on the input "6A45F!". The blocks function is used to "format" the output of toString into blocks of a given number of characters.

toBytes : String -> Maybe Bytes
Hex.Convert.toBytes "FF66"
    |> Maybe.map Hex.Convert.toString
--> Just "FF66"
toString : Bytes -> String

Do import Bytes.Encode as Encode exposing(encode). Then

encode (Encode.string "Hello")
    |> Hex.Convert.toString
--> "48656C6C6F" : String


Hex.Convert.toBytes "FF66!!"
    |> Maybe.map Hex.Convert.toString
--> Nothing : Maybe String
blocks : Int -> String -> List String

The blocks function is a general-purpose string utility which divides a string into blocks of characters, that is, a list of strings:

"abcdefhij" |> Hex.Convert.blocks 3
--> ["abc","def","hij"]