?
Solved

outputing to a binary file

Posted on 2003-11-07
6
Medium Priority
?
408 Views
Last Modified: 2013-11-15
I 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?
0
Comment
Question by:nofearse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 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
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9705654
BTW, just a partial solution, as I assume this is homework somehow :o)
0
 

Author Comment

by:nofearse
ID: 9705959
where is the filestream?
how does bit operator <<  work?
confused :S
0
 
LVL 1

Assisted Solution

by:dude_1967
dude_1967 earned 500 total points
ID: 9714397
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;
}
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10242616
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
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question