Little Endian Vs Big Endian
CPU like Intel, AMD, Apple Silicon, they store number in little-endian.
For example, the word example, have the following hex byte
00000000 65 78 61 6D:70 6C 65 example
This would store differently in little-endian since it store the small part of the number first
| Memory Address | Big-Endian (Motorola, PowerPC, Network) | Little-Endian (Intel, AMD, Apple M1/M2) |
|---|---|---|
00 | 65 ('e') | 65 ('e') |
01 | 78 ('x') | 6c ('l') |
02 | 61 ('a') | 70 ('p') |
03 | 6d ('m') | 6d ('m') |
04 | 70 ('p') | 61 ('a') |
05 | 6c ('l') | 78 ('x') |
06 | 65 ('e') | 65 ('e') |
#include <iostream>
int main() {
unsigned long hexNumber = 0x6578616d706c65;
char* toChar = (char*) &hexNumber;
std::cout << toChar << std::endl;
}
elpmaxe
[!note]
We need to use&hexNumberhere since0x6578616d706c65if we just do(char*) hexNumberthat would tell the computer to look at the memory address of a very big number, that would generate a core dump.In here we need to use the memory address of
hexNumberand cast that to a string.