Standard library · Encoding and data

encoding/binary

Writing and reading numbers as bytes, big-endian or little-endian.

import encoding.binary
On this page
  1. Types
  2. Functions

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
The options.
NameTypeDescription
big_endianbool = falseUse big-endian byte order; false by default.
encoding-binary-ReadWrite.salam
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)
end
Output/xI0NBIAAAAB/v8= 69 255 4660 4660 1 -2 7 8 9 -3 -4 -5 -6 -7 1.5 0.25 false

Functions

WriteU8

func WriteU8(b &: compress.ByteBuf, v: int)
Writes one unsigned byte.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteU16BE

func WriteU16BE(b &: compress.ByteBuf, v: int)
Writes an unsigned 16 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteU16LE

func WriteU16LE(b &: compress.ByteBuf, v: int)
Writes an unsigned 16 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteU32BE

func WriteU32BE(b &: compress.ByteBuf, v: int)
Writes an unsigned 32 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteU32LE

func WriteU32LE(b &: compress.ByteBuf, v: int)
Writes an unsigned 32 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteU64BE

func WriteU64BE(b &: compress.ByteBuf, v: i64)
Writes an unsigned 64 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteU64LE

func WriteU64LE(b &: compress.ByteBuf, v: i64)
Writes an unsigned 64 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteI16BE

func WriteI16BE(b &: compress.ByteBuf, v: int)
Writes a signed 16 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteI16LE

func WriteI16LE(b &: compress.ByteBuf, v: int)
Writes a signed 16 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteI32BE

func WriteI32BE(b &: compress.ByteBuf, v: int)
Writes a signed 32 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteI32LE

func WriteI32LE(b &: compress.ByteBuf, v: int)
Writes a signed 32 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteI64BE

func WriteI64BE(b &: compress.ByteBuf, v: i64)
Writes a signed 64 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

WriteI64LE

func WriteI64LE(b &: compress.ByteBuf, v: i64)
Writes a signed 64 bit integer little-endian, least significant byte first.

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)
Writes a 64 bit IEEE 754 float; 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)
Writes a 32 bit float; the value loses precision down to 32 bits as it is written.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadU8

func ReadU8(r &: compress.ByteReader): int
Reads one unsigned byte.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadU16BE

func ReadU16BE(r &: compress.ByteReader): int
Reads an unsigned 16 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadU16LE

func ReadU16LE(r &: compress.ByteReader): int
Reads an unsigned 16 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadU32BE

func ReadU32BE(r &: compress.ByteReader): int
Reads an unsigned 32 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadU32LE

func ReadU32LE(r &: compress.ByteReader): int
Reads an unsigned 32 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadU64BE

func ReadU64BE(r &: compress.ByteReader): i64
Reads an unsigned 64 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadU64LE

func ReadU64LE(r &: compress.ByteReader): i64
Reads an unsigned 64 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadI16BE

func ReadI16BE(r &: compress.ByteReader): int
Reads a signed 16 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadI16LE

func ReadI16LE(r &: compress.ByteReader): int
Reads a signed 16 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadI32BE

func ReadI32BE(r &: compress.ByteReader): int
Reads a signed 32 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadI32LE

func ReadI32LE(r &: compress.ByteReader): int
Reads a signed 32 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadI64BE

func ReadI64BE(r &: compress.ByteReader): i64
Reads a signed 64 bit integer big-endian, most significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadI64LE

func ReadI64LE(r &: compress.ByteReader): i64
Reads a signed 64 bit integer little-endian, least significant byte first.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam

ReadF64

func ReadF64(r &: compress.ByteReader, big_endian: bool): f64
Reads a 64 bit IEEE 754 float; big_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): f64
Reads a 32 bit float and returns it as a 64 bit float; big_endian picks the byte order.

The example for this item is shown with the ones above: encoding-binary-ReadWrite.salam