Extract 5 bits at a time from a stl string

A

Alan Ning

Hi all,

Say I have the following,

string str("Hello World\n")

I would like to extract 5 bits at a time from the string. I am doing
this to convert ascii string to another string type.

Is there a way to do this with stringstream and bitset?

I've been trying something like the following:

std::bitset<5> bsBit;

std::string strTemp = "hello, world\n";
std::stringstream ss;

ss << strTemp <<std::endl;
ss >> bsBit;

std::cout<<bsBit.to_ulong() <<std::endl;


It doesn't seem to work.

I am fairly new to stringstream, any comment is welcome.

Thanks

.... Alan
 
V

Victor Bazarov

Alan said:
Hi all,

Say I have the following,

string str("Hello World\n")

I would like to extract 5 bits at a time from the string. I am doing
this to convert ascii string to another string type.

Is there a way to do this with stringstream and bitset?

I've been trying something like the following:

std::bitset<5> bsBit;

std::string strTemp = "hello, world\n";
std::stringstream ss;

ss << strTemp <<std::endl;
ss >> bsBit;

std::cout<<bsBit.to_ulong() <<std::endl;


It doesn't seem to work.

I am fairly new to stringstream, any comment is welcome.

Thanks

... Alan

I think you need to use bitset<40>, stuff 5 chars into it (assuming
your 'char' is 8 bits), then extract 5 bits 8 times. Then move onto
the next 5 chars in your string. Repeat until the remaining length
is fewer than 5 chars, in which case you need to pad those.

V
 
D

Daniel T.

Alan Ning said:
Hi all,

Say I have the following,

string str("Hello World\n")

I would like to extract 5 bits at a time from the string. I am doing
this to convert ascii string to another string type.

Is there a way to do this with stringstream and bitset?

I've been trying something like the following:

std::bitset<5> bsBit;

std::string strTemp = "hello, world\n";
std::stringstream ss;

ss << strTemp <<std::endl;
ss >> bsBit;

std::cout<<bsBit.to_ulong() <<std::endl;


It doesn't seem to work.

I am fairly new to stringstream, any comment is welcome.

Your problem has nothing to do with stringstream. The put-to operator
for a bitset operates on a string of '0' and '1' characters, not
directly on bits.
 
A

Alan Ning

So looks like there is no way I can use stringstream and bitset to get
this to work.

My ultimate goal is to write a BitExtract class where it provides some
flexible API to extract any number of bits.

For example, I would like to call ExtractBit(unsigned int iNumBits) and
it will return a bitset that provides that many bits to me.

.... Alan
 
B

BobR

Alan Ning wrote in message ...
So looks like there is no way I can use stringstream and bitset to get
this to work.

PLEASE, do not top-post in this NG.

// - put #includes here -
// ------------
void ToBinary( const unsigned char val, std::eek:stream &out ){
for( int i(7); i >= 0; i-- )
if( val & ( 1 << i ) ) out << "1";
else out << "0";
} // ToBinary(const unsigned char, ostream&)
// ------------

int main(){
std::string Hello( "Hello World!" );
std::eek:stringstream binout;
for(size_t i(0); i < Hello.size(); ++i){
ToBinary( Hello.at( i ), binout );
}
std::string Hello2( binout.str() );
std::bitset<33> Bits( Hello2 ); // see Note below.

std::cout<<"Char="<<Hello.at( 0 )<<" Binary number "<<Hello2
<<" = Decimal Number "<< Bits.to_ulong() <<'\n';
}


/* -- output -- linefeeds inserted for newsreaders
Char=H Binary number 01001000011001010110110001101100011011
1100100000010101110110111101110010011011000110010000100001
= Decimal Number 2429212888
*/
Note: Yeah, it's "std::bitset<33>"! If I set it to a bigger size, windows
pukes.

Of course you should pass smaller chunks to the 'bitset', but, see how the
string 'Hello2' contained the conversion.
For the first 5 bits, you could use something like:

std::string Bits5( Hello2.substr(0, 5) );
std::bitset<5> Do5Bits( Bits5 );
std::cout<<" Binary number "<<Bits5<<" = Decimal Number "
<<Do5Bits.to_ulong()<<'\n';
// out: Binary number 01001 = Decimal Number 9
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top