The following may be of interest to you: (but may be way more than you want to deal with) A bin Manipulator For IOStreams
http://accu.org/index.php/journals/350
Alright, I read over the Creating "The bin Manipulator" section. I understand it except for the part at the end that states
Well, except that there is no easy way to turn binary formatting off again! Since we have replaced the formatting routine we can use the std::hex manipulator as often as we want: there will be no change at all.
I don't understand the consequences that might happen if its not turned off?
If this is the only way to do it I'm fine with it - I just don't understand the above quote properly.
Thanks so much for the help so far guestgulkan and Grey Wolf
///////////////////////////////////
//Template for using Binary calls//
///////////////////////////////////
std::string intToBinaryString(int in)
{
std::stringstream ss (std::stringstream::in | std::stringstream::out);
int mask = 0x8000;
for(int i = 0;i < 16; ++i)
{
if (in & mask)
ss << 1;
else
ss << 0;
mask = mask >> 1;
}
std::string str;
ss >> str;
std::string space = " ";
if(in < 128)
{
for(int i = 0; i < 9; i++)
{
str.erase((str.begin()+1) - 1);
}
str.insert(3, space);// Insert a space 3 characters into the string to
// break apart the binary digits if decimal < 128
}
if(in > 127) // && in < 256)
{
for(int i = 0; i < 8; i++)
{
str.erase((str.begin()+1) - 1);
}
str.insert(4, space);// Insert a space 4 characters into the string to
// break apart the binary digits if decimal > 127
}
return str;
}