Bytes.Encode
Bytes
Build Bytes from integers, floats, strings, and nested encoders.
BEAM-target encoders for OTP binaries.
Types
Encoder
type Encoder = EncoderExamples
Bytes.width (encode (unsignedInt8 7)) == 1Values
encode
encode : Encoder -> BytesTurn an encoder into Bytes.
Examples
Bytes.width (encode (unsignedInt8 7)) == 1
Bytes.width (encode (unsignedInt16 BE 7)) == 2
Bytes.width (encode (unsignedInt16 LE 7)) == 2signedInt8
signedInt8 : Int -> EncoderEncode an integer from -128 to 127 in one byte.
Examples
Bytes.width (encode (signedInt8 -3)) == 1signedInt16
signedInt16 : Endianness -> Int -> EncoderEncode an integer from -32768 to 32767 in two bytes.
Examples
Bytes.width (encode (signedInt16 BE -3)) == 2signedInt32
signedInt32 : Endianness -> Int -> EncoderEncode an integer from -2147483648 to 2147483647 in four bytes.
Examples
Bytes.width (encode (signedInt32 BE -3)) == 4unsignedInt8
unsignedInt8 : Int -> EncoderEncode an integer from 0 to 255 in one byte.
Examples
Bytes.width (encode (unsignedInt8 255)) == 1unsignedInt16
unsignedInt16 : Endianness -> Int -> EncoderEncode an integer from 0 to 65535 in two bytes.
Examples
Bytes.width (encode (unsignedInt16 LE 7)) == 2unsignedInt32
unsignedInt32 : Endianness -> Int -> EncoderEncode an integer from 0 to 4294967295 in four bytes.
Examples
Bytes.width (encode (unsignedInt32 BE 7)) == 4float32
float32 : Endianness -> Float -> EncoderEncode a 32-bit float in four bytes.
Examples
Bytes.width (encode (float32 BE 1.5)) == 4float64
float64 : Endianness -> Float -> EncoderEncode a 64-bit float in eight bytes.
Examples
Bytes.width (encode (float64 BE 1.5)) == 8bytes
bytes : Bytes -> EncoderCopy existing bytes into the encoder (no length prefix).
Examples
png imageData =
sequence
[ unsignedInt32 BE (Bytes.width imageData)
, bytes imageData
]string
string : String -> EncoderEncode a string as UTF-8 bytes (no length prefix).
Examples
Bytes.width (encode (string "hi")) == 2
Bytes.width (encode (string "hello")) == 5getStringWidth
getStringWidth : String -> IntUTF-8 byte width of a string (for length prefixes in your protocol).
Examples
getStringWidth "hi" == 2
getStringWidth "hello" == 5
getStringWidth "$20" == 3sequence
sequence : List Encoder -> EncoderConcatenate encoders in order.
Examples
type alias Ball =
{ x : Float
, y : Float
, z : Float
}
ball : Ball -> Encoder
ball { x, y, z } =
sequence
[ float32 BE x
, float32 BE y
, float32 BE z
]
Bytes.width (encode (sequence [ unsignedInt8 1, unsignedInt8 2 ])) == 2