Taking byte stream (1s & 0s) and storing them in a uint8_t array

I am trying to take this byte stream of information (1's and 0's) and im trying to store this in a uint8_t array (each index would be a byte long, 8 bits). I am having a hard time grasping how to store 8 bits into 1 index location for the given bitmapSize (For this example its 4).

So we would have a uint8_t array of size 4 with output looking like the following:
Array output --> 11111100 00011110 00001111 01

Any help would be appreciated!! Thank you!

1
2
3
4
5
6
7
8
9
10
11
  int allocatedMem[26] = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 };
  int bitmapSize = 0;
	
  for (int i = 0; i < sizeof(allocatedMem) / sizeof(allocatedMem[0]); i +=8)
  {
	bitmapSize++;
  }

  cout << "Size of bitmap: " << bitmapSize << endl;

Last edited on
You can do something like:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
uint8_t *binarize(const int *const input, const size_t input_size, size_t *const output_size)
{
    // allocate output buffer
    // note: required buffer length is the length of the input array divided by eight, but rounded up!
    *output_size = (input_size  + 7) / 8;
    uint8_t *const buffer = (uint8_t*) calloc(*output_size, sizeof(uint8_t));
    if (!buffer)
    {
        return NULL;
    }

    // fill the output buffer, one bit at a time, moving to the next output byte every eight input values (bits)
    uint8_t *out = buffer;
    for (size_t i = 0; i < input_size; ++i)
    {
        // move to next output byte, when the time has come
        if (i && (i % 8 == 0))
        {
            ++out;
        }
        // left-shift the current byte, by one, then set the least significant bit according to input
        *out = (*out << 1) | (input[i] ? 0x1 : 0x0); // <-- maps zero input to '0' and any non-zero input to '1'
    }

    // adjust bit positions of the final (incomplete) byte
    if (input_size % 8 != 0)
    {
        *out <<= 8 - (input_size % 8);
    }

    return buffer;
}

int main()
{
    const int input[26] = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 };

    // get length of the input array
    const size_t input_size = sizeof(input) / sizeof(input[0]);

    // convert input sequence to bytes
    size_t output_size;
    uint8_t *buffer = binarize(input, input_size, &output_size);
    if (!buffer)
    {
        return -1;
    }

    //print result
    for (size_t i = 0; i < output_size; ++i)
    {
        printf(i ? ",0x%02X" : "0x%02X", buffer[i]);
    }
    puts("");

    // free
    free(buffer);
}
Last edited on
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>
#include <iomanip>
using namespace std;

int main() {
    int a[] = {
        1, 1, 1, 1, 1, 1, 0, 0,
        0, 0, 0, 1, 1, 1, 1, 0,
        0, 0, 0, 0, 1, 1, 1, 1,
        0, 1
    };
    int asize = sizeof a / sizeof *a;
    int bsize = (asize + 7) / 8;
    auto b = new uint8_t[bsize];

    int j = 0;
    for (int i = 0; i < asize; ) {
        b[j] = (b[j] << 1) | a[i];
        if (++i % 8 == 0) ++j;
    }

    // I'm not sure if you need this part.
    b[j] <<= bsize * 8 - asize;

    cout << hex << setfill('0');
    for (int i = 0; i < bsize; ++i)
        cout << setw(2) << unsigned(b[i]) << '\n';

    delete[] b;
}

Output:
1
2
3
4
fc
1e
0f
40

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
#include <iostream>
#include <bitset>
#include <iomanip>

int main()
{
    std::bitset<8> stm{0};
    
    int data[]{1,1,1,1,1,1,0,0,  0,0,0,1,1,1,1,0,  0,0,0,0,1,1,1,1, 0,1};
    size_t LIMIT = sizeof(data)/sizeof(int);
    
    int COUNTER{0};
    while(COUNTER < LIMIT )
    {
        stm = 0;
        
        for(int i = 0; i < 8 && (COUNTER + i <  LIMIT) ; i++)
        {
            stm[7 - i] = data[COUNTER + i];
        }
        
        COUNTER += 8;
        
        std::cout
        << "bitset<8>: " << stm << ','
        << " hex: " << std::setw(2) << std::hex << stm.to_ulong() << ','
        << " dec: " << std::setw(3) << std::dec << stm.to_ulong() << '\n';
    }
    
    return 0;
}


bitset<8>: 11111100, hex: fc, dec: 252
bitset<8>: 00011110, hex: 1e, dec:  30
bitset<8>: 00001111, hex:  f, dec:  15
bitset<8>: 01000000, hex: 40, dec:  64
Program ended with exit code: 0
Topic archived. No new replies allowed.