BTW, just a partial solution, as I assume this is homework somehow :o)
Main Topics
Browse All TopicsI am trying to output a string to a binary file, but I have no idea how to do it at all
say if I have a string:
string A = "1010";
I want to output it as a byte to the binary file
and I need to format it as 00001010
I guess I need to extract it bit by bit from the string and somehow using bit operator to assign the bit to the byte, then output the byte to the binary file
But I can't find any reference on the web about that
Does anyone know how and give me an example of it?
Also, do I need to output it byte by byte to the binary file?
If I want to read a binary file and convert it back to a string or a character, how can I do it?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
nofear,
Here is an efficient way to get you started:
In my opionion, it would be wise to use STL-bitmaps to effectively complete the development goal (even if it is an assignment). Please look carefully at the following. All that is now needed is to put the byte into an output file stream. If you use this kind of technique in homework, then please try to gain rudimentary understanding of the STL-bitmaps before using them.
Sincerely, Chris.
// Sample code
#include <bitset>
#include <string>
int main(int argc, char* argv[])
{
// Use a STL-bitset created from a STL-string.
::std::string str("1010");
::std::bitset<8> b8(str);
// Now we can convert the bitset to the byte.
unsigned char by = static_cast<unsigned char>(b8.to_ulong());
// The file handling needs to be implemented...
return 0;
}
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Split: jkr {http:#9705654} & dude_1967 {http:#9714397}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: jkrPosted on 2003-11-07 at 19:48:06ID: 9705651
The easiest way would be to
#include <stdlib.h>
#include <string>
using namespace std;
//...
typedef unsigned char BYTE;
string A = "1010";
char* pcCnvEnd = NULL;
BYTE b = ( BYTE) strtol ( A.c_str (), &pcCnvEnd, 2);
if ( *pcCnvEnd) {
// some error occured
}