将嵌入在 8 位数组中的数据提取到不同大小的数组中
我正在开发一个项目,该项目接收一系列压缩为 8 位数组的参数。这些参数的大小可以不同,但始终以 8 位数组形式发送。目标是将这些参数提取到一个数组中,该数组的大小为最大参数的大小(事先已经知道)。
例如,参数数组的第一个索引中可能存储有两个 4 位参数,然后是一个 8 位参数,然后是一个 16 位参数。目标是将每个参数单独放置在长度为 4 的 16 位数组中,其中额外的空间只是 0。我正在努力寻找一种有效地执行此操作的方法,特别是因为 i2c 数组可能在数组的一个字节中嵌入多个参数。任何建议将不胜感激!
为了更好地说明这一点,我们的 8 位数据数组 = [0011 1011, 0110 1111],参数长度 = [2, 2, 4, 8]。然后,我希望结果数组显示为 [0000 0000, 0000 0011, 0000 1011, 0110 1111< /强>]。粗体数字表示从数据数组中获取的位。
I am working on a project which receives a series of parameters condensed into an 8 bit array. These parameters can come in varying sizes, but are always sent in an 8 bit array. The goal is to extract these parameters into an array the size of the largest sized parameter (which is already known beforehand).
For example, there could be two 4 bit parameters stored in the first index of the parameter array, then one 8 bit parameter, then one 16 bit parameter. The goal would then be to place each parameter separately in a 16 bit array of length 4, where extra space are just 0's. I am struggling finding a way to do this efficiently, especially since the i2c array could potentially have multiple parameters embedded within one byte of the array. Any suggestions would be greatly appreciated!
To better illustrate this, take our 8-bit data array = [0011 1011, 0110 1111] and take our parameter lengths to be = [2, 2, 4, 8]. Then, I would want the result array to appear as [0000 0000, 0000 0011, 0000 1011, 0110 1111]. The bold numbers indicate the bits taken from the data array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我之前通过简单地将接收到的字节数组视为位流来解决此类问题。
因此,很容易读取 3 位,然后是 7 位,然后是 10 位,然后是 2 位,等等,直到缓冲区末尾。
您只需保留当前位的索引:
获取下一位(0/1=位,-1=错误):
获取接下来的 N 位(最多 31 位,>=0 = OK,< ;0=错误):
如果
nbbits
(或仍要提取的位数) 大于或等于剩余位数在当前字节中。但如果您没有大量数据,则不需要它,如果您从 I²C 获取这些数据,则不太可能出现这种情况。字节序可能会使事情变得有点复杂,但这并非不可能解决 - 我通常在需要时直接在接待处进行字节序交换。
最后,你可以这样读取数据:
I solved this kind of problem before by simply considering the received array of bytes as a bit flow.
So it's quite easy to read 3 bits, then 7 bits, then 10 bits, then 2 bits, etc. until the end of the buffer.
You simply need to keep an index to the current bit:
To get the next bit (0/1=bit, -1=error):
To get the next N bits (max. 31 bits, >=0 = OK, <0=error):
This last function can be heavily optimized by getting directly all possible remaining bits of the current byte, if
nbbits
(or the number of bits still to extract) is greater or equal to the number of bits left in the current byte. But it shouldn't be needed if you don't have a huge load of data, which is quite unlikely if you get these data from I²C.Endianness can complexify things a bit, but it's not impossible to solve - I usually do the endianness swap directly at reception when needed.
Finally, you can read data this way:
在过去,人们常常使用结构来做到这一点。有人会过来告诉我这是未定义的,等等;这并不重要。为上面的代码生成糟糕的代码的编译器会为下面的代码生成良好的代码。现代优化编译器为上述问题找到了最有效的方法,但却会搞乱下面的事情。它内置了各种对齐方式和字节序假设;然而,在它起作用的地方,它运作良好。它不起作用的地方是未定义的。
In the old days people used to do this with structs. Somebody's going to come by and tell me this is undefined, etc; it doesn't really matter. A compiler that emits terrible code for the above emits good code for the below. It's the modern optimizing compilers that an figure the most efficient way for the above that will mess up the below. This has all kinds of alignment and endian assumptions built into it; however where it works, it works well. Where it doesn't work it's undefined.