如何将信息从8字节数组传递到可变位数据容器?

时间:2021-10-27 21:38:11

I have an 8 byte message where the differing chunks of the message are mapped to datums of different types (int, bool, etc.), and they vary in bit sizes (an int value is 12 bits in the message, etc.). I want to pass only the bits a datum is concerned with, but I am not sure if there is a better way. My current thoughts is to make a bit array type with a vector back end and have a templated accessor to get the value contained within to the type specified. Although as I am typing this I am starting to think a great big union of all the possible types could be passed to each datum.

我有一个8字节的消息,其中消息的不同块被映射到不同类型的数据(int,bool等),并且它们的位大小不同(int值在消息中是12位,等等)。我想只传递一个数据所涉及的位,但我不确定是否有更好的方法。我目前的想法是使用向量后端创建一个数组类型,并使用模板化访问器来获取指定类型中包含的值。虽然当我输入这个时,我开始认为所有可能类型的一个伟大的联合可以传递给每个数据。

EDIT:

The messages contain varying types of data. For example, one message contains an 8-bit int and 5 1-bit bools, while another message contains a 16-bit Timestamped (my own class) and an 8-bit int.

消息包含不同类型的数据。例如,一条消息包含一个8位int和5个1位bool,而另一条消息包含一个16位Timestamped(我自己的类)和一个8位int。

2 个解决方案

#1


Are the messages alway of the same format/order? Ie. 12bitsInt|8bitsChar|etc. If so a simple solution would be to set up appropriate bitmasks to grab each particular value. Ie. if the first 12 bits (low order) corresponded to an integer we could do:

这些消息的格式/顺序是否相同? IE浏览器。 12bitsInt | 8bitsChar |等。如果是这样,一个简单的解决方案就是设置适当的位掩码以获取每个特定值。 IE浏览器。如果前12位(低位)对应于我们可以做的整数:

__uint64 Message; // Obviously has data in it.
int IntPortion = Message & 0x00000111;

Which will copy the first 12 bits of the Message into the first 12 bits of your integer type. Set up appropriate bit masks for each chunk of the message and proceed. If the message format is not constant... well I would need you to elaborate maybe with an example message. Also the boost library has some nice bit manipulation classes:

这将将消息的前12位复制到整数类型的前12位。为消息的每个块设置适当的位掩码并继续。如果消息格式不是常数......那么我需要你详细说明一个示例消息。此外,boost库还有一些很好的位操作类:

Dynamic Bitset

Might be overkill if the format is constant though.

如果格式是恒定的,可能会有点矫枉过正。

#2


Have you looked at using a struct with explicit member sizes? For example, if you have a message where the field sizes are:

您是否看过使用具有显式成员大小的结构?例如,如果您有一个字段大小为的消息:

  • 1st field is 12 bits
  • 第一个字段是12位

  • 2nd field is 20 bits
  • 第二个字段是20位

  • 3rd field is 4 bits
  • 第3个字段是4位

  • ...

You could define a struct like:

你可以定义一个结构:

typedef struct {
  unsigned int field_1 : 12;
  unsigned int field_2 : 20;
  unsigned int field_3 : 4;
  /* etc */
} message_t;

Assuming that you have the message in a simple char array, either copy the data into a message_t struct or cast it to a message_t* :-

假设您在简单的char数组中有消息,请将数据复制到message_t结构中或将其转换为message_t *: -

char buffer[8] = /* however it's populated */
message_t* message_ptr = &buffer;
int field1 = message->field_1;
int field2 = message->field_2;

#1


Are the messages alway of the same format/order? Ie. 12bitsInt|8bitsChar|etc. If so a simple solution would be to set up appropriate bitmasks to grab each particular value. Ie. if the first 12 bits (low order) corresponded to an integer we could do:

这些消息的格式/顺序是否相同? IE浏览器。 12bitsInt | 8bitsChar |等。如果是这样,一个简单的解决方案就是设置适当的位掩码以获取每个特定值。 IE浏览器。如果前12位(低位)对应于我们可以做的整数:

__uint64 Message; // Obviously has data in it.
int IntPortion = Message & 0x00000111;

Which will copy the first 12 bits of the Message into the first 12 bits of your integer type. Set up appropriate bit masks for each chunk of the message and proceed. If the message format is not constant... well I would need you to elaborate maybe with an example message. Also the boost library has some nice bit manipulation classes:

这将将消息的前12位复制到整数类型的前12位。为消息的每个块设置适当的位掩码并继续。如果消息格式不是常数......那么我需要你详细说明一个示例消息。此外,boost库还有一些很好的位操作类:

Dynamic Bitset

Might be overkill if the format is constant though.

如果格式是恒定的,可能会有点矫枉过正。

#2


Have you looked at using a struct with explicit member sizes? For example, if you have a message where the field sizes are:

您是否看过使用具有显式成员大小的结构?例如,如果您有一个字段大小为的消息:

  • 1st field is 12 bits
  • 第一个字段是12位

  • 2nd field is 20 bits
  • 第二个字段是20位

  • 3rd field is 4 bits
  • 第3个字段是4位

  • ...

You could define a struct like:

你可以定义一个结构:

typedef struct {
  unsigned int field_1 : 12;
  unsigned int field_2 : 20;
  unsigned int field_3 : 4;
  /* etc */
} message_t;

Assuming that you have the message in a simple char array, either copy the data into a message_t struct or cast it to a message_t* :-

假设您在简单的char数组中有消息,请将数据复制到message_t结构中或将其转换为message_t *: -

char buffer[8] = /* however it's populated */
message_t* message_ptr = &buffer;
int field1 = message->field_1;
int field2 = message->field_2;