1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/encoding/binary
Alexander Medvednikov 6756d28595 all: 2023 copyright
2023-03-28 22:55:57 +02:00
..
big_endian_test.v encoding.binary: add functions to read/write with an offset and at the end of the array, split files by endianness (#15301) 2022-08-01 08:20:14 +03:00
big_endian.v all: 2023 copyright 2023-03-28 22:55:57 +02:00
little_endian_test.v encoding.binary: add functions to read/write with an offset and at the end of the array, split files by endianness (#15301) 2022-08-01 08:20:14 +03:00
little_endian.v all: 2023 copyright 2023-03-28 22:55:57 +02:00
README.md docs: unify format of notes (#17294) 2023-02-13 10:29:02 +02:00

Description

encoding.binary contains utility functions for converting between an array of bytes ([]u8) and unsigned integers of various widths (u16, u32, and u64).

There are two ways in which bytes can be encoded:

  1. Little endian: The least significant bytes are stored first, followed by the most significant bytes.
  2. Big endian: The most significant bytes are stored first, opposite to the little endian convention.

For example, let us take the number 0x12345678. In little endian, the bytes are extracted as 0x78, 0x56, 0x34, and 0x12. In big endian, the bytes are 0x12, 0x34, 0x56, and 0x78.

We follow a similar procedure when we want to go the other way around. Consider the second sequence of bytes in the previous example: 0x12, 0x34, 0x56, and 0x78. If we encode this sequence in little endian format, we get the integer 0x78563412. If we encode this sequence in big endian, we get 0x12345678.

Note

The functions in this module assume appropriately sized u8 arrays. If the sizes are not valid, the functions will panic.