包在一个字节串 [英] Pack in an octet string

查看:93
本文介绍了包在一个字节串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  填充字节串

我有不同的位长度的65参数,我需要填写字节串。 Parametrs将连续字节串来填补。例如,假设第一个参数是1位长,所以它将在字节串的第一个字节的第0位的位置来填补。现在,第二个参数是假设9位长。因此,这parametr的前7位将在同一个八比特填充和接下来的2位应该前往下一个八位位组的第0和第1位的位置。同样其他参数将得到填补的字节串。我试着写在那里我传递的指向当前八位位组的位位置,并从那里数据将被复制源指针的函数。但我发现在逻辑上实现的难度。我曾试过无数的逻辑(位操作,位移,旋转等),但无法获得正确的。我将不胜AP preciate,如果有人可以给我在C的逻辑/功能这样做。您还可以使用不同的函数原型。

I have 65 parameters of different bit length which I need to fill in an octet string. Parametrs would be filled continuously in octet string. For example, suppose the first parameter is 1 bit long so it would be filled at 0th bit position of 1st octet of the octet string. Now the second parameter is suppose 9 bit long. So the first 7 bits of this parametr would be filled in the same octet and the next 2 bits should go to 0th and 1st bit position of next octet. Similarly other parameters would get filled in the octet string. I tried to write a function where in I passed the pointer to the current octet, the bit position and the source pointer from where data would get copied. But I find difficulty in logic implemetation. I have tried numerous logics(bit operation, bit shifting, rotation etc.) but could not get the correct one. I would greatly appreciate if someone can give me a logic/function in "C" to do so. You can use different function prototype also.

推荐答案

这被称为比特流,而且是相当常见的如低级别的网络协议。

This is called a "bitstream", and is fairly common in e.g. low-level networking protocols.

如果有你的参数,例如一个合理的上限32位,你应该考虑在实施类似的条款:

If there's a reasonable upper bound on your parameters, e.g. 32 bits, you should think in terms of implementing something like:

void bitstream_append(uint32_t value, uint8_t bits);

这将追加 价值的最右边位到流。这可能有助于想到你会如何实施,如果你已经有了 bitstream_append_bit(布尔位);

which would append the bits rightmost bits of value to the stream. It might help to think how you would implement that if you already had bitstream_append_bit(bool bit);.

这篇关于包在一个字节串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆