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

#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.

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

原文链接: https://www.cnblogs.com/ToRapture/p/12858650.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Endianness

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/347457

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午4:32
下一篇 2023年3月2日 上午4:32

相关推荐