 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alexandros Guest
|
Posted: Fri Dec 26, 2003 12:52 pm Post subject: vector<bool> |
|
|
Can anyone tell me how to convert several bits stored in a vectro<bool>
to bytes (char)?
for example:
vector<bool> v;
v.reserve( ;
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
|
Posted: Fri Dec 26, 2003 1:24 pm Post subject: Re: vector<bool> |
|
|
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( ;
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
|
Posted: Fri Dec 26, 2003 3:44 pm Post subject: Re: vector<bool> |
|
|
"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
|
Posted: Fri Dec 26, 2003 3:45 pm Post subject: Re: vector<bool> |
|
|
"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
|
Posted: Fri Dec 26, 2003 6:40 pm Post subject: Re: vector<bool> |
|
|
"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( ;
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
|
Posted: Sun Dec 28, 2003 2:33 pm Post subject: Re: vector<bool> |
|
|
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
|
Posted: Sun Dec 28, 2003 3:33 pm Post subject: Re: vector<bool> |
|
|
"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 |
|
 |
|
|
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
|
|