문제

I've found myself recently fiddling with Modbus over IP in which data is stored and transferred in 16bit registers. To encode data types bigger than 16 bits (such as 32-bit floats, for example) the usual way is to split the 32bit value in two consecutive 16bit registers (a pair). Some implementors of the standard chose to put the most significant 16 bits in the first address of the pair, while some others chose to put them in the second. These two approaches are sometimes referred as Big Endian and Little Endian respectively.

At first I was a bit mislead by this wording, because CS textbooks usually state that endianess has to do with Byte Order. This is also reflected in the way some library functions treat endianess: for instance the struct.unpack library of Python unpacks little-endian and big-endian values by swapping the byte order.

However for this particular application (i.e. Modbus) endianess is indeed referring to the order of the consecutive 16bit registers (aka word order).

So what is the correct definition of endianess? Does it apply strictly to byte order or to any other size of the chunks into which values are split?

Thanks, Bye, Marco

도움이 되었습니까?

해결책

The packing of non-16 bit integer data was never defined by the original standard, and so you will find that different manufacturers have implemented packing in different ways, including how 4-byte floating point numbers are packed, and including what floating point format (IEEE-754 or other) is used. If you are attempting to code a general purpose solution you are going to have to provide user settable, or model number, options. If you are just doing a one-off solution, you have found it.

다른 팁

I amplify the previous. If you have a 2 (to start) byte integer,

 unsigned short XYZ = 0xBEAF ; // Ass-u-ming short is 2-bytes

is

*(char*) &XYZ 

0xBE

or

 0xAF???

The answer is always, it depends upon whether the PROCESSOR presumes the low order byte comes first (little endian = 0xAF) or whether the high order byte comes first (big endian = 0xBE). In theory you could have a single system with multiple processors (e.g. graphic and CPU) that had different endianness.

The Motorola 68000 chip series, SPARC, and IBM 360/370 are big endian systems. Intel 8086, PDP-11, and VAX are little endian systems.

Now SPARC is configurable to do both.

 Endianness is the byte order that the processor uses to interpret multi-byte data.

Endianness applies to any numeric data: Integers, floating point.

If I have

    unsigned long QXR = 0xDEADBEAF ;

as in your original problems is

*(unsigned short) &QXR

 0xDEAD

or

 0xBEAF ?

The ordering for multiple bytes is always the same as for single bytes. In a little endian system it is 0xBEAF and in a big endian system is is 0xDEAD.

It does not apply to single byte data (strings).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top