ktonon/elm-word/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Word.Bytes

Helper functions for creating lists of bytes.

import Byte
type alias ByteCount =
Int

Total number of bytes

fromInt : ByteCount -> Int -> List Byte

Split an integer value into a list of bytes with the given length.

fromInt 4 0
--> [ 0, 0, 0, 0 ] |> List.map Byte.fromInt


fromInt 4 1
--> [ 0, 0, 0, 1 ] |> List.map Byte.fromInt


fromInt 2 2
--> [ 0, 2 ] |> List.map Byte.fromInt


fromInt 1 255
--> [ 255 ] |> List.map Byte.fromInt


fromInt 4 256
--> [ 0, 0, 1, 0 ] |> List.map Byte.fromInt


fromInt 4 65537
--> [ 0, 1, 0, 1 ] |> List.map Byte.fromInt


fromInt 4 16777216
--> [ 1, 0, 0, 0 ] |> List.map Byte.fromInt


fromInt 8 72058693549555970
--> [ 1, 0, 1, 0, 0, 0, 1, 0 ] |> List.map Byte.fromInt
fromUTF8 : String -> List Byte

Convert a character into a list of bytes

fromUTF8 "a"
--> [ Byte.fromInt 97 ]


fromUTF8 "I ❤ cheese"
--> [ 73, 32,
-->   226, 157, 164,
-->   32, 99, 104, 101, 101, 115, 101 ]
--> |> List.map Byte.fromInt


fromUTF8 "dѐf"
--> [ 100, 209, 144, 102 ] |> List.map Byte.fromInt
fromHex : String -> List Byte

Attempt to interpret a string as a sequence of hexadecimal encoded bytes.

Fails for non-hex strings.

fromHex "not hex"
--> []

Each byte requires 2 characters, so odd length strings fail

fromHex "000"
--> []

Some passing examples

fromHex "00"
--> [ 0x00 ] |> List.map Byte.fromInt


fromHex "010203040506DEADbeef"
--> [ 0x01, 0x02, 0x03, 0x04
--> , 0x05, 0x06, 0xDE, 0xAD
--> , 0xBE, 0xEF
--> ] |> List.map Byte.fromInt
toHex : List Byte -> String

Convert a list of bytes to a string of hexadecimal characters.

[ 0xDE, 0xAD, 0xBE, 0xEF ]
    |> List.map Byte.fromInt
    |> toHex
--> "deadbeef"