Source Edit

This module contains helpers that deal with different byte orders (endian).

Endianness is the order of bytes of a value in memory. Big-endian means that the most significant byte is stored at the smallest memory address, while little endian means that the least-significant byte is stored at the smallest address. See also https://en.wikipedia.org/wiki/Endianness.

Unstable API.

Procs

  1. proc bigEndian16(outp, inp: pointer) {.inline, ...raises: [], tags: [], forbids: [].}

Copies inp to outp, storing it in 16-bit big-endian order. Both buffers are supposed to contain at least 2 bytes. Source Edit

  1. proc bigEndian32(outp, inp: pointer) {.inline, ...raises: [], tags: [], forbids: [].}

Copies inp to outp, storing it in 32-bit big-endian order. Both buffers are supposed to contain at least 4 bytes. Source Edit

  1. proc bigEndian64(outp, inp: pointer) {.inline, ...raises: [], tags: [], forbids: [].}

Copies inp to outp, storing it in 64-bit big-endian order. Both buffers are supposed to contain at least 8 bytes. Source Edit

  1. proc littleEndian16(outp, inp: pointer) {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Copies inp to outp, storing it in 16-bit little-endian order. Both buffers are supposed to contain at least 2 bytes. Source Edit

  1. proc littleEndian32(outp, inp: pointer) {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Copies inp to outp, storing it in 32-bit little-endian order. Both buffers are supposed to contain at least 4 bytes. Source Edit

  1. proc littleEndian64(outp, inp: pointer) {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Copies inp to outp, storing it in 64-bit little-endian order. Both buffers are supposed to contain at least 8 bytes. Source Edit

  1. proc swapEndian16(outp, inp: pointer) {.inline, noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Copies inp to outp, reversing the byte order. Both buffers are supposed to contain at least 2 bytes.

Example:

  1. var a = [1'u8, 2]
  2. var b: array[2, uint8]
  3. swapEndian16(addr b, addr a)
  4. assert b == [2'u8, 1]

Source Edit

  1. proc swapEndian32(outp, inp: pointer) {.inline, noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Copies inp to outp, reversing the byte order. Both buffers are supposed to contain at least 4 bytes.

Example:

  1. var a = [1'u8, 2, 3, 4]
  2. var b: array[4, uint8]
  3. swapEndian32(addr b, addr a)
  4. assert b == [4'u8, 3, 2, 1]

Source Edit

  1. proc swapEndian64(outp, inp: pointer) {.inline, noSideEffect, ...raises: [],
  2. tags: [], forbids: [].}

Copies inp to outp, reversing the byte order. Both buffers are supposed to contain at least 8 bytes.

Example:

  1. var a = [1'u8, 2, 3, 4, 5, 6, 7, 8]
  2. var b: array[8, uint8]
  3. swapEndian64(addr b, addr a)
  4. assert b == [8'u8, 7, 6, 5, 4, 3, 2, 1]

Source Edit