0

I have a structure that looks like:

typedef struct{
  float distance;
  float reflectivity;      
}data_point;

typedef struct{
  int flag;
  float Azimuth;
  data_point points[32];
}data_block;
  • A data point is represented in the packet by three bytes - two bytes of distance and one byte of calibrated reflectivity. The distance is an unsigned integer. It has 2 mm granularity. Hence, a reported value of 51,154 represents 102,308 mm or 102.308 m. Calibrated reflectivity is reported on a scale of 0 to 255. The elevation angle (ω) is inferred based on the position of the data point within a data block.
  • flag from float to 2 bytes(constant 0xFFEE)
  • A two-byte azimuth value (α) appears after the flag bytes at the beginning of each data block. The azimuth is an unsigned integer. It represents an angle in hundredths of a degree. Therefore, a raw value of 27742 should be interpreted as 277.42°

enter image description here

enter image description here

How can I create a vector of 100 bytes of binary data? ( 2 + 2 + (32x3))?

3
  • You can't assume that the classes will have no padding, as your calculation implies. Commented Sep 9, 2019 at 12:10
  • std::array<char,96> is 96 bytes. Commented Sep 9, 2019 at 12:15
  • One tedious option unaffected by compiler padding is to replace your structs with something that is exactly the size you desire (perhaps using a size setting constructor). Consider a std::vector<T>, where T might be uint8_t or char. Then add methods to read or write each field. When only a 'few' fields, this is not too bad, but it can grow quickly. A team I know used this approach when the 'other compiler' (sender vs receiver) was different, and either compiler padded differently. Commented Sep 9, 2019 at 12:34

1 Answer 1

2

You can either:

Sign up to request clarification or add additional context in comments.

3 Comments

So many solutions, I would also offer Google's Flattbuffers and Boost Serialization.
@tom Thank you for the contribution. If I'm not mistaking, flatbuffers are not by Google.
I think they were written by Wouter van Oortmerssen while working at Fun Propulsion Labs, Google. I could be wrong though...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.