Saving bytes from given bits

Hello guys, I have a question and hopefully you guys would help me.

I have an array of unsigned ints {0,1,1,1,1,0,1,1,1,1,0,0,0,0,0,0,1.....}

How can I grab eight bits at a time a save them as a byte??


Does saving them as a byte mean saving them as a unsigned char??

How can I have a byte array?///Does this even exist?

Or should I use unsigned char array?

Use std::vector<bool>
Neither will really help at all. (bitset only allows me to convert to 32-bit integer
assuming the bitset contains no more than 32 bits).

I'd write a loop.
jsmith wrote:
Neither will really help at all.

Oh, really?...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <bitset>
#include <sstream>
using namespace std;

int main()
{
    int i,j;

    int array[32]=
    {
        1,1,0,1,0,1,0,1,
        1,0,1,0,1,0,1,0,
        1,1,0,0,0,1,1,1,
        0,1,0,1,1,0,0,0
    };

    unsigned char result[4];

    for (i=0; i<4; i++)
    {
        stringstream buffer;

        for (j=0; j<8; j++)
            buffer << array[i*8+j];

        result[i]=bitset<8>(buffer.str()).to_ulong();
    }

    for (i=0; i<4; i++)
        cout << (int)result[i] << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

:P :D

EDIT: Ok, I have to admit this one is faster:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int main()
{
    int i,j;

    int array[32]=
    {
        1,1,0,1,0,1,0,1,
        1,0,1,0,1,0,1,0,
        1,1,0,0,0,1,1,1,
        0,1,0,1,1,0,0,0
    };

    unsigned char result[4]={0};

    for (i=0; i<4; i++)
    {
        for (j=0; j<8; j++)
            result[i]|=(array[i*8+j]<<(8-j-1));
    }

    for (i=0; i<4; i++)
        cout << (int)result[i] << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
Last edited on
You may also want to use bitset to store the bytes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <bitset>
using namespace std;

typedef bitset<8> Byte;

int main()
{
    int i,j;

    int array[32]=
    {
        1,1,0,1,0,1,0,1,
        1,0,1,0,1,0,1,0,
        1,1,0,0,0,1,1,1,
        0,1,0,1,1,0,0,0
    };

    Byte result[4];

    for (i=0; i<4; i++)
    {
        for (j=0; j<8; j++)
            result[i][8-j-1]=array[i*8+j];
    }

    for (i=0; i<4; i++)
    {
        cout << result[i].to_ulong();
        cout << " (";
        cout << result[i].to_string<char,
            char_traits<char>,allocator<char> >();
        cout << ')' << endl;
    }

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
I haven't tried it, but this seems a bit simpler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    static const int array[32]=
    {
        1,1,0,1,0,1,0,1,
        1,0,1,0,1,0,1,0,
        1,1,0,0,0,1,1,1,
        0,1,0,1,1,0,0,0
    };

    union {
        unsigned char ch[4];
        unsigned int result;
    } u;

    for( size_t i = 0; i < 32; ++i )
        u.result =  ( u.result << 1 ) | array[ i ];
}
Last edited on
It works! Nice! :) And just to be on the safe side you can make the union like this:

1
2
3
4
union {
    uint8_t ch[4];
    uint32_t result;
} u;

But I think the result you'll get here depends on endianness. But that's not a real problem. You just have to add some more code to take care of that.
Topic archived. No new replies allowed.