SLProject
4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
|
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) |
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
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.
src | the 16-bit number that should be converted |
dest | the pointer where the big-endian result will be written to |
Definition at line 22 of file ByteOrder.cpp.
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*)
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);
src | the 32-bit number that should be converted |
dest | the pointer where the big-endian result will be written to |
Definition at line 74 of file ByteOrder.cpp.
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.
src | the 64-bit number that should be converted |
dest | the pointer where the big-endian result will be written to |
Definition at line 89 of file ByteOrder.cpp.
void ByteOrder::writeBigEndian16 | ( | uint16_t | number, |
std::ostream & | stream | ||
) |
Converts a 16-bit number to big-endian and writes it to a stream
number | the number to be converted and written |
stream | the destination stream |
Definition at line 107 of file ByteOrder.cpp.
void ByteOrder::writeBigEndian32 | ( | uint32_t | number, |
std::ostream & | stream | ||
) |
Converts a 32-bit number to big-endian and writes it to a stream
number | the number to be converted and written |
stream | the destination stream |
Definition at line 118 of file ByteOrder.cpp.
void ByteOrder::writeBigEndian64 | ( | uint64_t | number, |
std::ostream & | stream | ||
) |
Converts a 64-bit number to big-endian and writes it to a stream
number | the number to be converted and written |
stream | the destination stream |
Definition at line 129 of file ByteOrder.cpp.