SLProject 4.0.000
ByteOrder Namespace Reference

Abort compilation if a char has not 8 bits, as functions for this case aren't implemented yet. More...

Functions

void toBigEndian16 (uint16_t src, char *dest)
 
void toBigEndian32 (uint32_t src, char *dest)
 
void toBigEndian64 (uint64_t src, char *dest)
 
void writeBigEndian16 (uint16_t number, std::ostream &stream)
 
void writeBigEndian32 (uint32_t number, std::ostream &stream)
 
void writeBigEndian64 (uint64_t number, std::ostream &stream)
 

Detailed Description

Abort compilation if a char has not 8 bits, as functions for this case aren't implemented yet.

Utility functions for functions related to byte order conversions

Function Documentation

◆ toBigEndian16()

void ByteOrder::toBigEndian16 ( uint16_t  src,
char *  dest 
)

Converts a 16-bit number from little-endian to big-endian regardless of the host byte order. See toBigEndian32() for an explanation of the algorithm.

Parameters
srcthe 16-bit number that should be converted
destthe pointer where the big-endian result will be written to

◆ toBigEndian32()

void ByteOrder::toBigEndian32 ( uint32_t  src,
char *  dest 
)

Converts a 32-bit number from little-endian to big-endian regardless of the host byte order. Here is an example that shows how the algorithm works:

Let's say we want to convert the number 8, which has the following memory layouts:

Little-endian: [8 0 0 0] Big-Endian: [0 0 0 8]

Now we take the address of the number and convert it to a char pointer to get access to the individual bytes of the number:

char* arr = (char*)&src;

This results in the following values for the array indices:

Little-endian: [0] => 8, [1] => 0, [2] => 0, [3] => 0 Big-endian: [0] => 0, [1] => 0, [2] => 0, [3] => 8

Next, we take all the array elements and shift them by (24 - index) bytes to the left:

uint32_t res = (uint32_t)arr[0] << 24 | (uint32_t)arr[1] << 16 | (uint32_t)arr[2] << 8 | (uint32_t)arr[3] << 0;

On a little-endian system, the 8 will be shifted 24 bits to the left and be the most significant byte, which is stored last in little-endian:

[0 0 0 8]

On a big-endian system, the 8 will not be shifted and remain the least significant byte, which is also stored last in big-endian:

[0 0 0 8]

We have thus achieved a host independent conversion to big-endian Finally, we copy the 4 bytes to the destination:

std::memcpy(dest, &res, 4);

Parameters
srcthe 32-bit number that should be converted
destthe pointer where the big-endian result will be written to

◆ toBigEndian64()

void ByteOrder::toBigEndian64 ( uint64_t  src,
char *  dest 
)

Converts a 64-bit number from little-endian to big-endian regardless of the host byte order. See toBigEndian32() for an explanation of the algorithm.

Parameters
srcthe 64-bit number that should be converted
destthe pointer where the big-endian result will be written to

◆ writeBigEndian16()

void ByteOrder::writeBigEndian16 ( uint16_t  number,
std::ostream &  stream 
)

Converts a 16-bit number to big-endian and writes it to a stream

Parameters
numberthe number to be converted and written
streamthe destination stream

◆ writeBigEndian32()

void ByteOrder::writeBigEndian32 ( uint32_t  number,
std::ostream &  stream 
)

Converts a 32-bit number to big-endian and writes it to a stream

Parameters
numberthe number to be converted and written
streamthe destination stream

◆ writeBigEndian64()

void ByteOrder::writeBigEndian64 ( uint64_t  number,
std::ostream &  stream 
)

Converts a 64-bit number to big-endian and writes it to a stream

Parameters
numberthe number to be converted and written
streamthe destination stream