当前位置:网站首页>Byte order problem

Byte order problem

2022-06-26 01:52:00 Look back in vain

Where there is network transmission, there is a byte order problem , The problem of byte count mainly exists in multi byte types Single byte has no byte order problem . Why can different memory have different storage methods , There are two mainstream Big end (big endian) And little end .

          At the big end The high order of a value has the status of an address   for example   0x16 78  stay int a This is how you keep it    16 78 

          The small end The low order of a value has the status of an address     for example   0x16 78 stay int a Zhongshi village 78 16 

remember Byte order only applies to multibyte , for example int long type Yes char Type has no function in network transmission and does not need to be switched .

But remember, union There may be an exception multibyte Terry in .

 union test {

   struct test_a {

    char a :3;

    char b: 5;

};

char d;

}test_t;

In this union Variable test_t in Because it's all char We can't even transmit use htoas htoal It's no use going around But its region is a Multi byte has byte order problem

for example our This is the case during transmission test_t.d=0b0110 1000

In the big end We can get   test_t.a == 011, test_t.b=0100;    Don't understand bit Whole upside down   I thought so 00010110 It's wrong.

And in the small end test_t.a=000, test_t.b=01101   

Be careful The reverse order of oneself is In bytes Immortal bit So the top 3 bit and 5 bit In their units It's not Whole bit    Reverse  

 

 

 

 

原网站

版权声明
本文为[Look back in vain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260004248094.html