converting from string to ints/bits

J

Jus!

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
 
D

David Hilsee

Jus! said:
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.
 
J

Jus!

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[j];

}

Any suggestions?
Thanks
 
D

David Hilsee

Jus! said:
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[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.
}
}
 
K

Kevin W.

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[j] = c - '0';

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

JKop

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
 
D

David Rubin

Jus! said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top