Solved

outputing to a binary file

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This article was originally published on Monitis Blog, you can check it here . If you have responsibility for software in production, I bet you’d like to know more about it. I don’t mean that you’d like an extra peek into the bowels of the sourc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

624 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