Moxa DA-662A-16-LX [50/63] Be careful when developing migrating programs

Moxa DA-662A-I-8-LX [50/63] Be careful when developing migrating programs
DA-662A Series Software Programmer's Guide
5-3
The difference between Big endian and Little endian
Big-endian and little-endian describe the order in which a sequence of bytes are stored in computer memory.
For big-endian, the big end (most significant value in the sequence) is stored first (i.e., at the lowest storage
address). For little-endian, the little end (least significant value in the sequence) is stored first. For example,
in a big-endian computer, the two bytes required for the hex number 1234 would be stored as 1234 in storage
(if 12 is stored at storage address 1000, for example, 34 will be at address 1001). In a little-endian system, it
would be stored as 3412 (34 at address 1000, 12 at 1001).
Be careful when developing/migrating programs
Normally, you do not need to consider the Endian nature (Endianness). However, in some circumstances you
do need to take it into account.
1. Data is transferred to another hardware platform or to the network.
2. If you will be transmitting 2 or more bytes of data at the same time (e.g., short, int, or long data types) you
must pay special attention to the Endian nature of the data convert if necessary.
Useful APIs for converting Big endian and Little endian
htonl(): Converts the unsigned integer hostlong from host byte order to network byte order.
htons(): Converts the unsigned short integer hostshort from host byte order to network byte order.
ntohl(): Converts the unsigned integer netlong from network byte order to host byte order.
ntohs(): Converts the unsigned short integer netshort from network byte order to host byte order.
Conversion Example
If a big endian computer wants to transfer data to a little endian computer, the structure of the data format is:
struct data_struct {
int a;
short b;
short c;
long d;
char e[4];
}
A: big endian computer
B: little endian computer
Assume int a=0x12345678. When the data is transferred from computer A to computer B, the content of int a
will be 0x78563412 if you do not do any conversions. Under the status, you can utilize ntohl(a) to convert
variable a to get the correct information.
NOTE
Click on the following links for more information about Endian:
https://en.wikipedia.org/wiki/Endianness
Transfer data

Содержание

Похожие устройства

Скачать