By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

converting from string to ints/bits

P: n/a
Hi.
I am reading bits(1's & 0's) from a file and i wa wondering what is the most
efficient method of converting these strings to individual int's?

eg. File contains:
110001
010011
etc...
Whats the best way to read in each line and break it up into individual
ints?
Thanks for you help.
Justin
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Jus!" <jm****@bigpond.com> wrote in message
news:lp*******************@news-server.bigpond.net.au...
Hi.
I am reading bits(1's & 0's) from a file and i wa wondering what is the most efficient method of converting these strings to individual int's?

eg. File contains:
110001
010011
etc...
Whats the best way to read in each line and break it up into individual
ints?


You have a text file that contains zeros and ones? That's odd. Do you
really mean that you are reading a binary file?

If you want to create integer values from some zeros and ones that are
specified in text form, then you may want to take a look at std::bitset. It
can be used to create strings from unsigned longs and vice versa. If the
zeroes and ones are specified on separate lines, then you can write a simply
program that uses std::getline to read the strings and std::bitset to
convert them.

--
David Hilsee
Jul 22 '05 #2

P: n/a
The file contains 1's ad 0's but i need to use them as ints.
Each line contains a string of ints (ie. 100101)
And i need to break that line up into separate ints (1 0 0 1 0 1 all
separate)

I tried to no avail:
instream >> data;

for(j = 0; j < x; j++)

{

stringstream sstream(data[j]);

sstream >> x_value[i][j];

}

Any suggestions?
Thanks
"David Hilsee" <da*************@yahoo.com> wrote in message
news:c-********************@comcast.com...
"Jus!" <jm****@bigpond.com> wrote in message
news:lp*******************@news-server.bigpond.net.au...
Hi.
I am reading bits(1's & 0's) from a file and i wa wondering what is the most
efficient method of converting these strings to individual int's?

eg. File contains:
110001
010011
etc...
Whats the best way to read in each line and break it up into individual
ints?


You have a text file that contains zeros and ones? That's odd. Do you
really mean that you are reading a binary file?

If you want to create integer values from some zeros and ones that are
specified in text form, then you may want to take a look at std::bitset.

It can be used to create strings from unsigned longs and vice versa. If the
zeroes and ones are specified on separate lines, then you can write a simply program that uses std::getline to read the strings and std::bitset to
convert them.

--
David Hilsee

Jul 22 '05 #3

P: n/a
"Jus!" <jm****@bigpond.com> wrote in message
news:uM******************@news-server.bigpond.net.au...
The file contains 1's ad 0's but i need to use them as ints.
Each line contains a string of ints (ie. 100101)
And i need to break that line up into separate ints (1 0 0 1 0 1 all
separate)

I tried to no avail:
instream >> data;

for(j = 0; j < x; j++)

{

stringstream sstream(data[j]);

sstream >> x_value[i][j];

}


Oh, I see. I didn't realize that you wanted to read the individual bit
values. I don't understand the code you gave (mostly because it was
incomplete), but it shouldn't be too difficult to process each bit value
individually:

#include <fstream>
#include <string>

std::ifstream file("file.txt");

if ( !file ) {
// Error opening file. Complain.
}
else {
std::string line;
while( std::getline(file, line) ) {
// Individually process every character on the line
for ( std::string::iterator it = line.begin(); it != line.end();
++it ) {
char c = *it;
if ( c != '0' && c != '1' ) {
// The character read was not a zero or one.
// Complain about invalid input.
}
else {
int num = c - '0';
// Do something with the zero or one stored in "num"
}
}
}

if ( !file.eof() ) {
// Not all of the lines in the file were read. Complain.
}
}

--
David Hilsee
Jul 22 '05 #4

P: n/a
> And i need to break that line up into separate ints (1 0 0 1 0 1 all
separate)

I tried to no avail:
instream >> data;


This will probably read the whole line into data. You need to read one
char at a time with the get() member.
c = instream.get();
x_value[i][j] = c - '0';

where c is a char, instream is an ifstream and x_value[i][j] is an int.
You'll probably also need to make sure you don't read nondigits into c.

--
Kevin W :-)
Opera/CSS/webdev blog: http://www.exclipy.com/
Using Opera: http://www.opera.com/m2/
Jul 22 '05 #5

P: n/a
Jus! posted:
Hi.
I am reading bits(1's & 0's) from a file and i wa wondering what is the
most efficient method of converting these strings to individual int's?

eg. File contains:
110001
010011
etc...
Whats the best way to read in each line and break it up into individual
ints?
Thanks for you help.
Justin

Here's just a general example:
unsigned char ActualValue(char *p_input)
{
unsigned char value = 0;

if (p_input[0] == '1') value += 128;
if (p_input[1] == '1') value += 64;
if (p_input[2] == '1') value += 32;
if (p_input[3] == '1') value += 16;
if (p_input[4] == '1') value += 8;
if (p_input[5] == '1') value += 4;
if (p_input[6] == '1') value += 2;
if (p_input[7] == '1') value += 1;

return value;
}
int main()
{
char blah[] = "00111011";

ActualValue(blah);
}
-JKop
Jul 22 '05 #6

P: n/a
JKop posted:
unsigned char ActualValue(char *p_input)


unsigned char ActualValue(const char* const p_input)

-JKop
Jul 22 '05 #7

P: n/a
"Jus!" <jm****@bigpond.com> wrote in message news:<lp*******************@news-server.bigpond.net.au>...
Hi.
I am reading bits(1's & 0's) from a file and i wa wondering what is the most
efficient method of converting these strings to individual int's?

eg. File contains:
110001
010011
etc...
Whats the best way to read in each line and break it up into individual
ints?


Stream into bitset<>. /david
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.