SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
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

Definition at line 22 of file ByteOrder.cpp.

23 {
24  char* arr = (char*)&src;
25  uint16_t res = (uint16_t)arr[0] << 8 |
26  (uint16_t)arr[1];
27  memcpy(dest, &res, 2);
28 }

◆ 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*)

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

Definition at line 74 of file ByteOrder.cpp.

75 {
76  char* arr = (char*)&src;
77  uint32_t res = (uint32_t)arr[0] << 24 |
78  (uint32_t)arr[1] << 16 |
79  (uint32_t)arr[2] << 8 |
80  (uint32_t)arr[3];
81  memcpy(dest, &res, 4);
82 }

◆ 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

Definition at line 89 of file ByteOrder.cpp.

90 {
91  char* arr = (char*)&src;
92  uint64_t res = (uint64_t)arr[0] << 56 |
93  (uint64_t)arr[1] << 48 |
94  (uint64_t)arr[2] << 40 |
95  (uint64_t)arr[3] << 32 |
96  (uint64_t)arr[4] << 24 |
97  (uint64_t)arr[5] << 16 |
98  (uint64_t)arr[6] << 8 |
99  (uint64_t)arr[7];
100  memcpy(dest, &res, 8);
101 }

◆ 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

Definition at line 107 of file ByteOrder.cpp.

108 {
109  char buffer[2];
110  toBigEndian16(number, buffer);
111  stream.write(buffer, 2);
112 }
void toBigEndian16(uint16_t src, char *dest)
Definition: ByteOrder.cpp:22

◆ 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

Definition at line 118 of file ByteOrder.cpp.

119 {
120  char buffer[4];
121  toBigEndian32(number, buffer);
122  stream.write(buffer, 4);
123 }
void toBigEndian32(uint32_t src, char *dest)
Definition: ByteOrder.cpp:74

◆ 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

Definition at line 129 of file ByteOrder.cpp.

130 {
131  char buffer[8];
132  toBigEndian64(number, buffer);
133  stream.write(buffer, 8);
134 }
void toBigEndian64(uint64_t src, char *dest)
Definition: ByteOrder.cpp:89