Variable sized type

M

me

Hi there.

I'm writing some code to interpret binary data.

The spec of the data is in the form of field names and field sizes.

The design is to take a byte array of data, and a struct representing
the fields, memcpy the first onto the second, then access the data:

e.g

struct DataType
{
Type1 DataA ;
Type2 DataB ;
Type7 DataC ;
};

Would represent data that has a field of 1 byte, then a field of 2
bytes, then a field of 7 bytes. I read 10 bytes, memcpy the data onto an
instance of DataType, then access the data through DataA,B,C etc...

This would be easy if the data was either 1,2,4,or 8 bytes, but its
not...some are 3, some are 7, some are 5

Is there a simple way of defining an integer type that is n bytes long?

cheers
 
V

Victor Bazarov

me said:
I'm writing some code to interpret binary data.

The spec of the data is in the form of field names and field sizes.

The design is to take a byte array of data, and a struct representing
the fields, memcpy the first onto the second,
Yuck!

then access the data:

e.g

struct DataType
{
Type1 DataA ;
Type2 DataB ;
Type7 DataC ;
};

Would represent data that has a field of 1 byte, then a field of 2
bytes, then a field of 7 bytes. I read 10 bytes, memcpy the data onto an
instance of DataType,

Don't. Allocate memory of needed size.
then access the data through DataA,B,C etc...

Don't. Provide access using functions. Do necessary offsetting in the
functions.
This would be easy if the data was either 1,2,4,or 8 bytes, but its
not...some are 3, some are 7, some are 5

Is there a simple way of defining an integer type that is n bytes long?

No.

V
 
A

Arijit

me said:
Hi there.

I'm writing some code to interpret binary data.

The spec of the data is in the form of field names and field sizes.

The design is to take a byte array of data, and a struct representing
the fields, memcpy the first onto the second, then access the data:

e.g

struct DataType
{
Type1 DataA ;
Type2 DataB ;
Type7 DataC ;
};

Would represent data that has a field of 1 byte, then a field of 2
bytes, then a field of 7 bytes. I read 10 bytes, memcpy the data onto an
instance of DataType, then access the data through DataA,B,C etc...

This would be easy if the data was either 1,2,4,or 8 bytes, but its
not...some are 3, some are 7, some are 5

Is there a simple way of defining an integer type that is n bytes long?

cheers

No. Even if there were, what you are suggesting would not work due to padding.
You are better off converting the data into integers using shifts and adds.

-Arijit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top