Byte order

xiaoxiao2021-03-06  31

For different software and hardware, in addition to sigh, there will be more emotions, such a phenomenon is caused by various reasons, but I always hate, bytes The order is such a list. Simply put, the word sequence is to process numbers, machines and actual digital high-low order, that is,: numbers: 0x10 00 20 Little-Endian machine: 20 00 10 Big-endian machine: 10 00 20 This difference is CPU is determined, the current popular CPU is based on IBM's Power-PC is BIG-Endian, and the other is basically Little-Endian. Such differences lead to problems when they exchange data in the heterogeneous platform, of course, exchange data between the heterogeneous platform is a sensitive problem, there are many ways to use, not discussed here. However, there will be such a problem when programming, look at this situation:

Code: * ((Short *) "ab") >> 8

result:

Code: Little-endian = 'b' Big-endian = 'a'

This statement can be used to determine the machine word sequence. If the exchange data between the two platforms, the exchange data must be converted, for example, for int type: BIG-ENDIAN Write file

Code: INT i = 100; Write (FD, & I, SIZEOF (INT));

Little-Endian after reading

Code: INT i; read (fd, & i, sizeof (int)); char buf [sizeof (int)); Memcpy (buf, & i, sizeof (int)); for (i = 0; i

This is just a list, directly transmitting binary data directly between algorithms, even if there is no word sequence, is not a wise method, as an optional method is to use text to exchange data, which can at least avoid word sequence The problem. Many encryption algorithms take the conversion between strings and numbers to pursue speed, after calculation, you must pay attention to the problem of word sequence, you can see the use of pre-compiled methods in some implementations, so It is inconvenient. If you use the previous statement to judge, you can automatically adapt.

转载请注明原文地址:https://www.9cbs.com/read-81315.html

New Post(0)