C/C++ 将字符数组视为比特流的代码

发布于 2024-07-08 13:37:37 字数 387 浏览 6 评论 0原文

我在 char[] 数组中有一大堆二进制数据,我需要将其解释为打包的 6 位值的数组。

可以坐下来编写一些代码来做到这一点,但我认为必须有人已经编写了一个很好的现有类或函数。

我需要的是这样的:

int get_bits(char* data, unsigned bitOffset, unsigned numBits);

所以我可以通过调用获取数据中的第 7 个 6 位字符:

const unsigned BITSIZE = 6;
char ch = static_cast<char>(get_bits(data, 7 * BITSIZE, BITSIZE));

I have a big lump of binary data in a char[] array which I need to interpret as an array of packed 6-bit values.

I could sit down and write some code to do this but I'm thinking there has to be a good extant class or function somebody has written already.

What I need is something like:

int get_bits(char* data, unsigned bitOffset, unsigned numBits);

so I could get the 7th 6-bit character in the data by calling:

const unsigned BITSIZE = 6;
char ch = static_cast<char>(get_bits(data, 7 * BITSIZE, BITSIZE));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

祁梦 2024-07-15 13:37:37

这可能不适用于大于 8 的大小,具体取决于字节序系统。 这基本上就是 Marco 发布的内容,尽管我不完全确定他为什么一次只收集一点。

int get_bits(char* data, unsigned int bitOffset, unsigned int numBits) {
    numBits = pow(2,numBits) - 1; //this will only work up to 32 bits, of course
    data += bitOffset/8;
    bitOffset %= 8;
    return (*((int*)data) >> bitOffset) & numBits;  //little endian
    //return (flip(data[0]) >> bitOffset) & numBits; //big endian
}

//flips from big to little or vice versa
int flip(int x) {
    char temp, *t = (char*)&x;
    temp = t[0];
    t[0] = t[3];
    t[3] = temp;
    temp = t[1];
    t[1] = t[2];
    t[2] = temp;
    return x;
}

This may not work for sizes greater than 8, depending on endian system. It's basically what Marco posted, though I'm not entirely sure why he'd gather one bit at a time.

int get_bits(char* data, unsigned int bitOffset, unsigned int numBits) {
    numBits = pow(2,numBits) - 1; //this will only work up to 32 bits, of course
    data += bitOffset/8;
    bitOffset %= 8;
    return (*((int*)data) >> bitOffset) & numBits;  //little endian
    //return (flip(data[0]) >> bitOffset) & numBits; //big endian
}

//flips from big to little or vice versa
int flip(int x) {
    char temp, *t = (char*)&x;
    temp = t[0];
    t[0] = t[3];
    t[3] = temp;
    temp = t[1];
    t[1] = t[2];
    t[2] = temp;
    return x;
}
那小子欠揍 2024-07-15 13:37:37

我认为以下内容可能有效。

int get_bit(char *data, unsigned bitoffset) // returns the n-th bit
{
    int c = (int)(data[bitoffset >> 3]); // X>>3 is X/8
    int bitmask = 1 << (bitoffset & 7);  // X&7 is X%8
    return ((c & bitmask)!=0) ? 1 : 0;
}

int get_bits(char* data, unsigned bitOffset, unsigned numBits)
{
    int bits = 0;
    for (int currentbit = bitOffset; currentbit < bitOffset + numBits; currentbit++)
    {
        bits = bits << 1;
        bits = bits | get_bit(data, currentbit);
    }
    return bits;
}

我尚未调试或测试它,但您可以使用它作为起点。

另外,还要考虑位顺序。 您可能需要

    int bitmask = 1 << (bitoffset & 7);  // X&7 is X%8

进行更改。

    int bitmask = 1 << (7 - (bitoffset & 7));  // X&7 is X%8

根据位数组的生成方式

I think something in the line of the following might work.

int get_bit(char *data, unsigned bitoffset) // returns the n-th bit
{
    int c = (int)(data[bitoffset >> 3]); // X>>3 is X/8
    int bitmask = 1 << (bitoffset & 7);  // X&7 is X%8
    return ((c & bitmask)!=0) ? 1 : 0;
}

int get_bits(char* data, unsigned bitOffset, unsigned numBits)
{
    int bits = 0;
    for (int currentbit = bitOffset; currentbit < bitOffset + numBits; currentbit++)
    {
        bits = bits << 1;
        bits = bits | get_bit(data, currentbit);
    }
    return bits;
}

I've not debugged nor tested it, but you can use it as a start point.

Also, take into account bit order. You might want to change

    int bitmask = 1 << (bitoffset & 7);  // X&7 is X%8

to

    int bitmask = 1 << (7 - (bitoffset & 7));  // X&7 is X%8

depending on how the bit array has been generated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文