0%

Endianness

What is endianness?

Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored first.

How to check

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;


string get_endian() {
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x1020;

if (sizeof(short) != 2)
return "sizeof(short) != 2";

if (un.c[0] == 0x20 && un.c[1] == 0x10)
return "little-endian";
if (un.c[0] == 0x10 && un.c[1] == 0x20)
return "big-endian";
return "unknown";

}

int main(int argc, char **argv) {
cout << "endian: " << get_endian() << "\n";
return 0;
}

Network byte order

https://stackoverflow.com/a/997586/13133551

"Network byte order" is Big Endian, and protocols such as TCP use this for integer fields (e.g. port numbers). Functions such as htons and ntohs can be used to do conversion.

The data itself doesn't have any endianness it's entirely application defined.

As we can see from the picture, the endianness of net is Big Endian.