vector<bool>

A

Alexandros

Can anyone tell me how to convert several bits stored in a vectro<bool>
to bytes (char)?

for example:
vector<bool> v;
v.reserve(8);
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(1);
v.push_back(0);
v.push_back(0);
v.push_back(0);

and now I want to get
char* c = static_cast<char*>(v.begin());
so that
c[0] == 8;

that above doesn't work. How can I do it?

Is there any funcion "toByte()" or sth like that?

thanks.
 
L

lallous

Hello,

Alexandros said:
Can anyone tell me how to convert several bits stored in a vectro<bool>
to bytes (char)?

for example:
vector<bool> v;
v.reserve(8);
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(1);
v.push_back(0);
v.push_back(0);
v.push_back(0);

and now I want to get
char* c = static_cast<char*>(v.begin());
so that
c[0] == 8;

that above doesn't work. How can I do it?

Is there any funcion "toByte()" or sth like that?

thanks.

The code should be similar to this:
char c = 0;
for (int i=0;i<v.length();i++)
{
c = (c << i) | (v == true ? 1 : 0);
}
 
C

Cy Edmunds

Alexandros said:
Can anyone tell me how to convert several bits stored in a vectro<bool>
to bytes (char)?

for example:
vector<bool> v;
v.reserve(8);
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(0);
v.push_back(1);
v.push_back(0);
v.push_back(0);
v.push_back(0);

and now I want to get
char* c = static_cast<char*>(v.begin());
so that
c[0] == 8;

that above doesn't work. How can I do it?

Is there any funcion "toByte()" or sth like that?

thanks.

std::vector<bool> blivvet;
blivvet.push_back(true);
blivvet.push_back(false);
blivvet.push_back(false);
blivvet.push_back(true);
std::vector<char> snarfle(blivvet.begin(), blivvet.end());
for (unsigned int j = 0; j < snarfle.size(); ++j)
std::cout << int(snarfle[j]);
std::cout << '\n';

output is:
1001
 
A

Alexandros

output is:
1001

But according to what I want to do output should be 9.

I mean: if i have this vector<bool> v: (0,0,0,0,1,0,0,1)

I want to get a vector<char> or a char* vc: (9)

another example:
if v is (0,0,0,0,1,0,0,1, 0,0,0,0,0,0,1,1)
then vc should be (9,3)

do I make sense?
 
V

Victor Bazarov

Alexandros said:
But according to what I want to do output should be 9.

I mean: if i have this vector<bool> v: (0,0,0,0,1,0,0,1)

I want to get a vector<char> or a char* vc: (9)

another example:
if v is (0,0,0,0,1,0,0,1, 0,0,0,0,0,0,1,1)
then vc should be (9,3)

do I make sense?

Probably.

There is no pret-a-porte way for you to use. You have to
program it yourself.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top