Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

outputing to a binary file

Posted on 2003-11-07
6
Medium Priority
?
413 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
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Eseutil Hard Recovery is part of exchange tool and ensures Exchange mailbox data recovery when mailbox gets corrupt due to some problem on Exchange server.
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

885 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