473,396 Members | 1,608 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,396 software developers and data experts.

converting from string to ints/bits

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
7 2392
"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
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
"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
> 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
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
JKop posted:
unsigned char ActualValue(char *p_input)


unsigned char ActualValue(const char* const p_input)

-JKop
Jul 22 '05 #7
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
3
by: Chris N. Hinds | last post by:
I have a question regarding accessing long long ints in unions. I have constructed a union with a double, two ints in a structure, and a long long int. When the double is loaded with a...
8
by: Rick | last post by:
Hi, Does C have some handy functions to convert chars, ints and floats to bit arrays? I need to store that stuff binary so a few functions would be great. Converting chars and ints isn't...
24
by: Skybuck Flying | last post by:
Ok, I am getting really sick of all these integers types. Not only in C... but mostly in C. WHY DOESN'T ANSI MAKE THESE TYPES A STANDARD sint8 uint8
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
12
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of...
8
by: 116Rohan | last post by:
I came across a question in one of the Computing olympiad regarding string pattern matching. Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
15
by: David Geering | last post by:
Is there any standard as to what a long and int mean or is it compiler dependent? Or furthermore architecture dependent? I know a short short long int isn't actually a recognised type, but if you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.