While little-endian is indeed more common in modern CPUs, big-endian still has important applications. Let's explore the main use cases:
The most significant use of big-endian is in network protocols. This format is so common in networking that it's often called "network byte order":
#include "SDL.h"
void SendPacket(SDL_RWops* Network,
uint32_t Value) {
// Use big-endian for network protocols
SDL_WriteBE32(Network, Value);
}
void SendPlayerData(SDL_RWops* Network,
uint16_t Level,
uint32_t Score) {
// Network protocols typically use big-endian
SDL_WriteBE16(Network, Level);
SDL_WriteBE32(Network, Score);
}
Many established file formats use big-endian for historical reasons or compatibility:
#include "SDL.h"
bool IsPNGFile(SDL_RWops* Handle) {
// PNG files start with a big-endian signature
uint32_t Signature{SDL_ReadBE32(Handle)};
// PNG magic number
return Signature == 0x89504E47;
}
void WriteMIDIEvent(SDL_RWops* Handle,
uint32_t DeltaTime, uint8_t Event) {
// MIDI files use big-endian
SDL_WriteBE32(Handle, DeltaTime);
SDL_WriteU8(Handle, Event);
}
Common file formats using big-endian include:
Big-endian has some natural advantages:
Example showing why big-endian can be more readable:
#include <iomanip>
#include <iostream>
#include "SDL.h"
void CompareFormats() {
uint32_t Value{0x12345678};
SDL_RWops* Handle{
SDL_RWFromFile("compare.bin", "wb")};
if (!Handle) { return; }
SDL_WriteBE32(Handle, Value);
SDL_WriteLE32(Handle, Value);
SDL_RWclose(Handle);
std::cout << "Hex dump of file:\n"
<< "Big-endian : 12 34 56 78\n"
<< "Little-endian: 78 56 34 12\n";
}
int main() { CompareFormats(); }
The key takeaway is that neither endianness is inherently "better" - they each have their use cases. When developing cross-platform applications, it's important to:
Answers to questions are automatically generated and may not have been reviewed.
Learn how to handle byte order in using SDL's endianness functions