Solved

outputing to a binary file

Posted on 2003-11-07
6
393 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 125 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 125 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

697 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