C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

vector<bool>

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Alexandros
Guest





PostPosted: Fri Dec 26, 2003 12:52 pm    Post subject: vector<bool> Reply with quote



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

for example:
vector<bool> v;
v.reserve(Cool;
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.

Back to top
lallous
Guest





PostPosted: Fri Dec 26, 2003 1:24 pm    Post subject: Re: vector<bool> Reply with quote



Hello,

"Alexandros" <email (AT) company (DOT) com> wrote

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

for example:
vector v.reserve(Cool;
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 {
c = (c << i) | (v[i] == true ? 1 : 0);
}

--
Elias



Back to top
Ron Natalie
Guest





PostPosted: Fri Dec 26, 2003 3:44 pm    Post subject: Re: vector<bool> Reply with quote




"lallous" <lallous (AT) lgwm (DOT) org> wrote


Quote:
c = (c << i) | (v[i] == true ? 1 : 0);

c = (c << 1) | v[i];


Back to top
lallous
Guest





PostPosted: Fri Dec 26, 2003 3:45 pm    Post subject: Re: vector<bool> Reply with quote

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:

"lallous" <lallous (AT) lgwm (DOT) org> wrote


c = (c << i) | (v[i] == true ? 1 : 0);

c = (c << 1) | v[i];

Yes....

--
Elias



Back to top
Cy Edmunds
Guest





PostPosted: Fri Dec 26, 2003 6:40 pm    Post subject: Re: vector<bool> Reply with quote

"Alexandros" <email (AT) company (DOT) com> wrote

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

for example:
vector v.reserve(Cool;
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

--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
Alexandros
Guest





PostPosted: Sun Dec 28, 2003 2:33 pm    Post subject: Re: vector<bool> Reply with quote


Quote:

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?


Back to top
Victor Bazarov
Guest





PostPosted: Sun Dec 28, 2003 3:33 pm    Post subject: Re: vector<bool> Reply with quote

"Alexandros" <email (AT) company (DOT) com> wrote...
Quote:


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?

Probably.

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

Victor



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.