1

Can someone help me to understand the following text:

Both-byte orders

A numerical value represented by the hexadecimal representation (st uv wx yz) shall be recorded in an eight-byte field as (yz wx uv st st uv wx yz).

NOTE: For example, the decimal number 305419896 has (12 34 56 78) as its hexadecimal representation and is recorded as (78 56 34 12 12 34 56 78).

What does this mean for reading the value ? Do I simply get the 32-bit as an uint32 and that's all or do I need to convert something in order to get the correct value? Or do I only extract the 4 bytes from the 8 byte field in order to get the value?

EDIT: Would this work in a union like this?

union test { uint64 fullValue; uint8 FirstFourBytes[4]; uint8 SecondFourBytes[4]; } 

And then I access the SecondFourBytes Array to get the correct value.

1
  • 1
    With a union, the arrays overlap. They don't stack up, so FirstFourBytes and SecondFourBytes will contain the exact same data. Commented Oct 9, 2013 at 17:54

1 Answer 1

4

The byte order refers to the order in which the individual bytes that make up a larger data type (such as a 32-bit integer) are stored in memory.

Traditionally a 32-bit integer is written out either in Little Endian or Big Endian byte order in memory, taking up 4 bytes (32 bits) but it looks like in your case the protocol dictates that the integer be stored in both Little Endian and Big Endian order one after each other, doubling the space required for storage to 64 bits.

When reading, you will have to take into account that the value uses 8 bytes, and you can read either the first 4 bytes (little endian), or the last 4 bytes (big endian), depending on which endianness (byte order) your platform uses.

When writing, you will have to serialize both versions, writing out the little endian representation first, followed by the big endian representation.

UPDATE 2

Your modified union would still not work because you are now attempting to create a union between a uint64 presumably 64-bit data type and two other arrays, each taking 4 bytes (32 bits).

// this is not right union test { uint64 fullValue; uint8 FirstFourBytes[4]; // this points to the first 4 bytes uint8 SecondFourBytes[4]; // .. despite name, this also points to the first 4 bytes } 

However, you could use a union like this:

union test { uint64 fullValue; // although this is probably useless to you struct Raw { uint8 LittleEndianBytes[4]; uint8 BigEndianBytes[4]; }; } 
Sign up to request clarification or add additional context in comments.

4 Comments

First: Thank you very much for your help. I am still not sure if I understand the difference between your code and my code. What does the struct do differently ?
@JeckyPorter - You're welcome. The struct stores the two arrays sequentially, one after the other. The union is more like an alias, for the same memory location.
@JeckyPorter The difference is that a union is only as large as the largest member... so in your union you have 3 values starting at the same address, where in miky's you have 3 valuesm, full value and littleEndianBytes start at the same address, and BigEndian starts 4 bytes later.
You don't really need the union. In this case a single struct is all you need. You just need to write the code to map a 32-bit integer to and from the struct, manually.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.