1

Is there a quick way to convert an array of uint8_t to a biteset.

uint8_t test[16]; 
// Call a function which populates test[16] with 128 bits
function_call(& test);
for(int i=0; i<16; i++)
  cout<<test[0]; // outputs a byte
cout<<endl;
std:: bitset<128> bsTest;

I tried this but does not work

bsTest(test);
CPP_NEW
  • 197
  • 2
  • 9
  • Works fine for me. – DeiDei May 04 '16 at 18:01
  • I am getting this error "error: no match for call to ‘(std::bitset<128ul>) (uint8_t [16])’" – CPP_NEW May 04 '16 at 18:04
  • @DeiDei: It would "work" in the sense that if `test` was zeroed, some compilers might use the string based constructor, determine it was the empty string (the first byte is `NUL`), and zero out the `bitset`; coincidentally correct if `test` is zeroed, but wrong in all other cases. – ShadowRanger May 04 '16 at 18:04
  • @ShadowRanger I assumed that was true when I wrote my comment. It seems the OP is getting a compiler error, which I couldn't reproduce. – DeiDei May 04 '16 at 18:05
  • @ShadowRanger that was byte array to biteset. – CPP_NEW May 04 '16 at 18:07
  • @DeiDei In my program I am populating the test[] and then trying to convert it into bitset. I first tried the one mentioned by ShadowRanger but I could not fix – CPP_NEW May 04 '16 at 18:08
  • @CPP_NEW `test[]` must be populated only by `'0'`'s and `'1'`'s. If `test[]` contains anything else, the constructor will throw `std::invalid_argument`. – DeiDei May 04 '16 at 18:10
  • @DeiDei Yes, it is populating with 0s and 1s. When I printed the values in a for loop, it gave me 128 bits (11101011001110100011110001011110000011100101001111000111100000000010111011100111100010111100110001011010100001001011010000110011) – CPP_NEW May 04 '16 at 18:11
  • 1
    @CPP_NEW Edit the question with the actual code that's giving you a problem. – DeiDei May 04 '16 at 18:16
  • @DeiDei I am calling a function which populates the test[]. I edited the original question. It is not possible to put the entire function there. – CPP_NEW May 04 '16 at 18:21
  • You need to post a [mcve] – Paul R May 04 '16 at 18:40
  • Are you doing `std::bitset<128> bsTest(test);` or only `bsTest(test);`. The latter is obviously not going to work. – DeiDei May 04 '16 at 18:47
  • @CPP_NEW: There is no C++ `byte` type; the other question was likely referring to `char`, `unsigned char` or `uint8_t` (all of which represent bytes); it's a direct equivalent. – ShadowRanger May 04 '16 at 20:31
  • @ShadowRanger You are right !! I am kind of good C programmer but trying C++ – CPP_NEW May 04 '16 at 20:42

1 Answers1

2

I propose you a possible solution.

Not so good, not so quick, a little dirty but I hope it can help.

#include <bitset>
#include <iostream>

int main ()
 {
   uint8_t  test[16] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                         'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p' };

   std::bitset<128> bsTest { } ;

   for ( unsigned ui = 0 ; ui < 16 ; ++ui )
    {
      bsTest <<= 8;

      std::bitset<128> bsTmp { (unsigned long) test[ui] };

      bsTest |= bsTmp;
    }

   std::cout << bsTest;

   return 0;
 }

The idea is initialize the bitset to zero

std::bitset<128> bsTest { } ;

and add a uint8_t at a time at the end of another bitset

std::bitset<128> bsTmp { (unsigned long) test[ui] };

then merge (bit or) the two bitsets

bsTest |= bsTmp;

and shift 8 bit the result

bsTest <<= 8;

p.s.: sorry for my bad English

max66
  • 65,235
  • 10
  • 71
  • 111
  • Thanks a lot !! It is somehow working for me now. I will see if I can make it quicker. BTW your English is as good as (or better than) mine :) – CPP_NEW May 04 '16 at 20:19