Byte ordering

The architectures differ in the way they store integers in their memory. There are two methods: little-endian and big-endian. Little-endian is an order in which the least significant byte ("little end") is stored first, whereas big-endian is an order in which the most significant byte in the sequence ("big end") is stored first (at the lowest storage address).

Some problems may occur when working within different architectures. One of example is the use of USB Protocol (its specification defines data records in little-endian) and SCSI Protocol, which de facto uses big-endian convention.

Standard Library enables a flexible transition from one format to another. It is realized through the set of macros that convert these data from specified format to format used by CPU and vice versa. There are four types of converting macros:

  • stCPUToLE* - converts number format specific for CPU into little-endian,
  • stCPUToBE* - converts number format specific for CPU into big-endian,
  • stLEToCPU* - converts number format from little-endian into format used by CPU,
  • stBEToCPU* - converts number format from big-endian into format used by CPU

(*) - the data size in bytes - 16, 32 or 64, accessible after setting AR_USE_64_BITfor 1.

Thanks to macros, during conversion, the operation is performed only when two architectures are engaged. If, for example, the stLEToCPU32 is used, on the processor already working in little-endian, it will be performed the simple value assignment.

The library is configured to work on processor working in little-endian mode by default. The addition of architecture specific files should automatically fit the library to work on the right platform. When macros do not work properly, it is necessary to set AR_LENDIAN_CPUmanually for 1 in case processor works in little-endian, or for 0 if it works in big-endian mode.

The additional function stIsLEndianCPU, enables to check if the processor is consistent with little-endian. It is however excluded from compilation, and to activate it ST_IS_LENDIAN_CPU_FUNC should be set for 1.

If endianness will not in usage, as only one architecture is used, this part of library can be excluded from compilation by setting ST_USE_ENDIANS for 0, to reduce the output code.

SpaceShadow documentation