473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Use stand-alone function in cout-statement

Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number as-is,
no grouping). I came up with:

void
output_binary_string(ostream& os, const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}
}

Now, what if I want to use this function in a cout-statement? Do I have to
create a class or struct then and make my function the overloaded operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;

Thanks for any replies
Jul 22 '05 #1
3 2730
Eric Lilja wrote:
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number as-is,
no grouping). I came up with:

void
output_binary_string(ostream& os, const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}
}

Now, what if I want to use this function in a cout-statement? Do I have to
create a class or struct then and make my function the overloaded operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;


An easy way would be

# include <iostream>
# include <string>

class output_binary_string
{
private:
std::string s_;

public:

output_binary_string(const std::string &s)
: s_(s)
{
}

void print(std::ostream &stream) const
{
stream << s_; // just format it as you wish
}
};

std::ostream &operator<<(std::ostream &stream,
const output_binary_string &obs)
{
obs.print(stream);

return stream;
}
int main()
{
std::string s = "101001010";

std::cout << "Binary: " << output_binary_string(s) << std::endl;
}

You could work out something with manipulators also. Let us know if you
need help.

Jonathan
Jul 22 '05 #2

"Eric Lilja" <er*************@yahoo.com> wrote in message
news:cq**********@news.island.liu.se...
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number as-is,
no grouping). I came up with:

void
string
output_binary_string(ostream& os, const string& binary_string)
binary_string(const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */
ostringstream os;
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}
return os.str();
}

Now, what if I want to use this function in a cout-statement?

cout << binary_string("10010010") < '\n';
Do I have to
create a class or struct then and make my function the overloaded operator<< for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;

Thanks for any replies


-Mike
Jul 22 '05 #3

"Mike Wahler" wrote:

"Eric Lilja" <er*************@yahoo.com> wrote in message
news:cq**********@news.island.liu.se...
Hi, I need a function that expects a const std::string& containing the
visual representation of a number in binary format. It should then output
this number in groups of four bits separated by a space. If the length of
the string is not divisable by four (normally, it should receive 8-bit
numbers (that is, strings of length 8), it should output the number
as-is,
no grouping). I came up with:

void


string
output_binary_string(ostream& os, const string& binary_string)


binary_string(const string& binary_string)
{
/* If the length of the string (i.e., the number of bits in *
* the binary number), isn't divisable by four, simply output *
* the entire string and return. */


ostringstream os;
if(binary_string.length() % 4)
{
os << binary_string;

return;
}

string::size_type index = 0;

for(string::size_type i = 0; i < binary_string.length() / 4; ++i)
{
for(int j = 0; j < 4; j++)
{
os << binary_string[index++];
}

os << ' ';
}


return os.str();
}

Now, what if I want to use this function in a cout-statement?

cout << binary_string("10010010") < '\n';
Do I have to
create a class or struct then and make my function the overloaded

operator<<
for that class/struct?
Right now I do:
cout << "binary_representation: ";
output_binary_string(cout, s);
cout << endl;

I would like to be able to cout << "binary representation: " <<
output_binary_string(cout, s) << endl;

Thanks for any replies


-Mike


Thanks both Mike and Jonathan for taking the time to help me out! I now
consider this problem solved and I've moved my attention to other issues in
this program. Thanks again.

/ Eric
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AstrA | last post by:
Hi All My forms are gonig to have a dark to green background and I want my labels to stand out. I'm gonig to use white for the colour of the actual labels, but you see a lot of the...
25
by: Ben | last post by:
Hi all! I learned Python as part of my university coursework... I enjoyed it. Now I'm just wondering how Python compares with asp, jsp, php and what not?? I don't have slightest knowledge about...
1
by: gicio | last post by:
HI! I have a little problem: I have change the administrator password (Windows 2003 Server) which MS SQL 2000 use to login. And now the SQL server can't stand up. What should I do? thx!
0
by: Peter Sands | last post by:
Hi, I have setup a stand-by db2 DB server, I want to test the log shipping method. Do I just issue a 'list history..' on the source server to get the logs I need to ship. Then copy those logs...
1
by: Tony Do | last post by:
Hi every, Which version of DB2 (Work group Or Enterprise) can I use as a stanby server? Under Window Do I need Windows server enterprise version?
1
by: E. Robert Tisdale | last post by:
Acronym Finder: http://it.acronymfinder.com/af-query.asp?string=exact&acronym=rtfm
6
by: Robert Smith | last post by:
I am programming C in linux and i just need a quick and dirty way to make some text stand out. I would like to change the color maybe to red. I want to print out something like printf("Blah:...
5
by: John Creighton | last post by:
What does BCPL stand for is it one language or a family of languages. My bust guess it the B C Programming Languages. But that is just a short in the dark. I am also not sure what languages fall...
4
by: Greg | last post by:
Regarding extensibility, e.g. EnvDTE what does "DTE" stand for? -- Greg McPherran www.McPherran.com
1
by: =?Utf-8?B?U3R1YXJ0TA==?= | last post by:
The PC running Windows XP Home SP2 is put into Stand By. After coming out of Stand By, I try to start an internet connection, using Tiscali Broadband. A dialogue box is displayed with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.