473,414 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Convert string to binary

I want to read a file then save it's binary data as a .txt with one's and
zero's representing the actual file's bits. So it could be later
reconstructed from that txt file. As of yet, I read the file into a string.

I've tried something with bitset but I can't seem to have it read a string.
Jul 23 '05 #1
4 24685
On Mon, 31 Jan 2005 03:14:12 +0100 in comp.lang.c++, Matej Barac
<ma*********@gmail.com> wrote,
I want to read a file then save it's binary data as a .txt with one's and
zero's representing the actual file's bits. So it could be later
reconstructed from that txt file. As of yet, I read the file into a string.

I've tried something with bitset but I can't seem to have it read a string.


What happened when you tried?
Where is your code?

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[5.8] How do I post a question about code that doesn't work
correctly?" It is always good to check the FAQ before posting. You
can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Compare:

#include <iostream>
#include <iterator>
#include <bitset>
using namespace std;
int main()
{
std::copy(istream_iterator<char>(cin),
istream_iterator<char>(),
ostream_iterator<bitset<8> >(cout, "\n"));
}
Jul 23 '05 #2
On Mon, 31 Jan 2005 02:30:26 GMT, David Harmon wrote:
On Mon, 31 Jan 2005 03:14:12 +0100 in comp.lang.c++, Matej Barac
<ma*********@gmail.com> wrote,
I want to read a file then save it's binary data as a .txt with one's and
zero's representing the actual file's bits. So it could be later
reconstructed from that txt file. As of yet, I read the file into a string.

I've tried something with bitset but I can't seem to have it read a string.


What happened when you tried?
Where is your code?

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[5.8] How do I post a question about code that doesn't work
correctly?" It is always good to check the FAQ before posting. You
can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Compare:

#include <iostream>
#include <iterator>
#include <bitset>
using namespace std;
int main()
{
std::copy(istream_iterator<char>(cin),
istream_iterator<char>(),
ostream_iterator<bitset<8> >(cout, "\n"));
}


Sorry, but as I said I don't have any (working) code except the read/write
file part. I have some trouble with turning a string into a bitset.

Shouldn't the below work?

string input; // read from input file
bitset<10> bit((string) input);
cout << bit;

Compiles but produces a runtime error.
Jul 23 '05 #3
David Harmon wrote:
std::copy(istream_iterator<char>(cin),
istream_iterator<char>(),
ostream_iterator<bitset<8> >(cout, "\n"));


Note that this will remove whitespaces or, more precisely, the bits
representing them: 'std::istream_iterator<T>' uses the formatted input
operations to read the data. By default, the formatted input functions
skip leading whitespace, even if they just read a single character.
Also, there is no guarantee that 'char' has exactly 8 bits: the
standard only guarantees that it has at least 8 bits. You might want to
use this instead:

/**/ std::copy(std::istreambuf_iterator<char>(std::cin) ),
/**/ std::istreambuf_iterator<char>(),
/**/ std::ostream_iterator<
/**/ std::bitset<std::numeric_limits<char>::digits>
/**/ >(std::cout, "\n"));
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4
On Mon, 31 Jan 2005 04:33:29 +0100 in comp.lang.c++, Matej Barac
<ma*********@gmail.com> wrote,
Shouldn't the below work?

string input; // read from input file
bitset<10> bit((string) input);
cout << bit;


A string is a sequence of characters. You want to convert the
characters one by one, not the whole string at once.

//(E.g. Using a clunky loop instead of std::copy)

for(int i=0; i<input.size(); ++i) {
cout << bitset<8>(input[i]) << '\n';
}

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
3
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a...
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
10
by: steve.lorimer | last post by:
I'm looking for a pre-processor command that will allow me to resolve const strings into const char literals at compile time. Looking at the code below, I can take 5 characters and create a...
5
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I am having a problem converting string to binary and I am hoping someone can help me out I have a sql query that does an update that updates a binary field calles password ...
1
by: svnlakshmi | last post by:
Pls help me out in this aspect--How to convert a binary file to a file in text format using C?
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
0
by: jcarter | last post by:
How can I create a CodeDOM statement to accomplish the following this.pictureBox.Image = <Convert the binary value of the picture from the XML file and display> I have tried the following...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.