encoding/binary
Writing and reading numbers as bytes, big-endian or little-endian.
import encoding.binary
Binary file formats and network protocols store a number as several bytes in a row. Big-endian writes the most significant byte first and is the usual order in network protocols; little-endian is the other way round and is what most processors use, and formats such as ZIP.
The writing functions take a ByteBuf and the reading functions take a ByteReader, both from the compress package.
Types
BinaryOptions
struct BinaryOptions| Name | Type | Description |
|---|---|---|
big_endian | bool = false | Use big-endian byte order; false by default. |
import encoding.binary
import compress
func main:
mut b := compress.NewByteBuf()
binary.WriteU8(b, 255)
binary.WriteU16BE(b, 4660)
binary.WriteU16LE(b, 4660)
binary.WriteU32BE(b, 1)
binary.WriteI16LE(b, -2)
println compress.BufToBase64(b)
binary.WriteU32LE(b, 7)
binary.WriteU64BE(b, 8)
binary.WriteU64LE(b, 9)
binary.WriteI16BE(b, -3)
binary.WriteI32BE(b, -4)
binary.WriteI32LE(b, -5)
binary.WriteI64BE(b, -6)
binary.WriteI64LE(b, -7)
binary.WriteF64(b, 1.5, true)
binary.WriteF32(b, 0.25, false)
println compress.BufLen(b)
mut r := compress.ReaderFromBuf(b)
println binary.ReadU8(r), binary.ReadU16BE(r), binary.ReadU16LE(r), binary.ReadU32BE(r), binary.ReadI16LE(r)
println binary.ReadU32LE(r), binary.ReadU64BE(r), binary.ReadU64LE(r)
println binary.ReadI16BE(r), binary.ReadI32BE(r), binary.ReadI32LE(r), binary.ReadI64BE(r), binary.ReadI64LE(r)
println binary.ReadF64(r, true), binary.ReadF32(r, false)
o := binary.BinaryOptions {}
println o.big_endian
compress.BufFree(b)
endFunctions
WriteU8
func WriteU8(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteU16BE
func WriteU16BE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteU16LE
func WriteU16LE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteU32BE
func WriteU32BE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteU32LE
func WriteU32LE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteU64BE
func WriteU64BE(b &: compress.ByteBuf, v: i64)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteU64LE
func WriteU64LE(b &: compress.ByteBuf, v: i64)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteI16BE
func WriteI16BE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteI16LE
func WriteI16LE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteI32BE
func WriteI32BE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteI32LE
func WriteI32LE(b &: compress.ByteBuf, v: int)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteI64BE
func WriteI64BE(b &: compress.ByteBuf, v: i64)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteI64LE
func WriteI64LE(b &: compress.ByteBuf, v: i64)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteF64
func WriteF64(b &: compress.ByteBuf, v: f64, big_endian: bool)big_endian picks the byte order.The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
WriteF32
func WriteF32(b &: compress.ByteBuf, v: f64, big_endian: bool)The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadU8
func ReadU8(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadU16BE
func ReadU16BE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadU16LE
func ReadU16LE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadU32BE
func ReadU32BE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadU32LE
func ReadU32LE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadU64BE
func ReadU64BE(r &: compress.ByteReader): i64The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadU64LE
func ReadU64LE(r &: compress.ByteReader): i64The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadI16BE
func ReadI16BE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadI16LE
func ReadI16LE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadI32BE
func ReadI32BE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadI32LE
func ReadI32LE(r &: compress.ByteReader): intThe example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadI64BE
func ReadI64BE(r &: compress.ByteReader): i64The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadI64LE
func ReadI64LE(r &: compress.ByteReader): i64The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadF64
func ReadF64(r &: compress.ByteReader, big_endian: bool): f64big_endian picks the byte order.The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam
ReadF32
func ReadF32(r &: compress.ByteReader, big_endian: bool): f64big_endian picks the byte order.The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam