Endianness defines the location of byte 0 within a larger data structure.

Little-endian

The least significant byte of a 16-bit value is sent or stored before the most significant byte

Bits7:0 | Bits15:8

A 32bit value is stored in a byte array as:

Byte[3] | Byte[2] | Byte[1] | Byte[0]

Big-endian

The more significant byte of a 16-bit value is sent or stored before the less significant byte

Bits15:8 | Bits7:0

A 32bit value is stored in a byte array as:

Byte[0] | Byte[1] | Byte[2] | Byte[3]

Endian usage

Windows – Little endian

Modbus – Big Endian

Little Endian

UInt32 conversion

UInt16[1] | UInt16[0]

UInt64 conversion

UInt16[3] | UInt16[2] | UInt16[1] | UInt16[0]

Big Endian

UInt32 conversion

UInt16[0] | UInt16[1]

UInt64 conversion

UInt16[0] | UInt16[1] | UInt16[2] | UInt16[3]

Float conversion

UInt16[0] | UInt16[1]


	//Converting big endian modbus registers to float on little endian windows (ModbusRegisterValues[0] was received first)
	array<Byte>^byteArray = gcnew array<Byte>(4);
	byteArray[3] = (Byte)(ModbusRegisterValues[0] >> 8);
	byteArray[2] = (Byte)(ModbusRegisterValues[0] & 0x00ff);
	byteArray[1] = (Byte)(ModbusRegisterValues[1] >> 8);
	byteArray[0] = (Byte)(ModbusRegisterValues[1] & 0x00ff);
	float my_float = BitConverter::ToSingle(byteArray, 0);

	//Converting float on little endian windows to big endian modbus registers (ModbusRegisterValues[0] will be sent first)
	ModbusRegisterValues = gcnew array <UInt16>(2);
	array<Byte>^byteArray = BitConverter::GetBytes(my_float_value);
	ModbusRegisterValues[0] = ((UInt16)byteArray[3] << 8) | ((UInt16)byteArray[2]);		//Converting float on little endian windows to big endian modbus registers
	ModbusRegisterValues[1] = ((UInt16)byteArray[1] << 8) | ((UInt16)byteArray[0]);

 

 

 

UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.