Solved

outputing to a binary file

Posted on 2003-11-07
6
368 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
6 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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 125 total points
Comment Utility
BTW, just a partial solution, as I assume this is homework somehow :o)
0
 

Author Comment

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

Assisted Solution

by:dude_1967
dude_1967 earned 125 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now