Re: All combinations of 0's and 1's in 128 bit character array?
Quote:
Originally Posted by
itsmeandnobodyelse
The vector already holds an internal 16-byte elements array.
If you do
Code:
unsigned char * p = (unsigned char *)&v[0];
some_otherfunction_expecting_a_16bytes_array(p);
where v is one of the permutations you got the pointer p would point to a 16-elements array.
If you want to copy the array to fixed-sized char array, you would/could do
Code:
unsigned char arr[16] = { '\0' };
unsigned char * p = (unsigned char *)&v[0];
memcpy(arr, p, min(sizeof(arr), v.size() * sizeof(v[0])));
The min is only to make sure that the vector really has 16 elements of type char, same as the arr.
Regards, Alex
That is all very true, but the problem remains that if you store bytes inside the vector, when you call next_permutation, you will be per mutating bytes, and not bits. Sure, you can recover the chars easily, but they won't be properlly shifted
A good solution would have been to use a vector<bool>, as it stores bit(-like) objects. This would work perfectly if you could write &v[0] on a vector bool, but you can't (at least not to my knowledge).
IMO, it's a lose-lose situation, where you have to get creative and daisy chain several solutions.
Re: All combinations of 0's and 1's in 128 bit character array?
Quote:
Originally Posted by
monarch_dodra
That is all very true, but the problem remains that if you store bytes inside the vector, when you call next_permutation, you will be per mutating bytes, and not bits.
So what? Make your container 8x larger, and you'll have logically the same result.
Re: All combinations of 0's and 1's in 128 bit character array?
Quote:
Originally Posted by
Lindley
So what? Make your container 8x larger, and you'll have logically the same result.
Yes, if you make your container 8 times larger, you get the desired result, but from there, it's the copying back into the char array that becomes problematic :/
Re: All combinations of 0's and 1's in 128 bit character array?
Quote:
Originally Posted by
itsmeandnobodyelse
The vector already holds an internal 16-byte elements array.
If you do
Code:
unsigned char * p = (unsigned char *)&v[0];
some_otherfunction_expecting_a_16bytes_array(p);
where v is one of the permutations you got the pointer p would point to a 16-elements array.
If you want to copy the array to fixed-sized char array, you would/could do
Code:
unsigned char arr[16] = { '\0' };
unsigned char * p = (unsigned char *)&v[0];
memcpy(arr, p, min(sizeof(arr), v.size() * sizeof(v[0])));
The min is only to make sure that the vector really has 16 elements of type char, same as the arr.
Regards, Alex
:( the index should be 15 :cry:
Re: All combinations of 0's and 1's in 128 bit character array?
Hmm, actually, using bitset instead of a vector and then just memcpying into a char array of length 16 (bytes) works :)
Thanks all!
Re: All combinations of 0's and 1's in 128 bit character array?
Yikes. You shouldn't use memcpy() on C++ objects.
If it's just an assignment, whatever. But if you actually intend this code to be portable and robust, that's not a good approach.