Converting a string to a binary byte

Hi!
I'm totally new to this forum and pretty new at c/c++ too. My problem right now is that I have a file with a byte written in each row (example: 01011001 in row 1 and 00011001 in row 2). I want to read this into my program in order to make bit operations on the bytes. This is how far I have gotten:

#include <iostream>
#include <fstream>
using namespace std;
int b;
int main () {
string line;
ifstream myfile ("enfil.txt");
if (myfile.is_open())
{
int i=0;
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
i++;
}
myfile.close();
cin >> b;
}

else cout << "Unable to open file";

return 0;
}


this will print out everything in the console but how do I convert my variable line to the desired byte?
[quote
My problem right now is that I have a file with a byte written in each row[
(example: 01011001 in row 1 and 00011001 in row 2
/quote]

if your file is like what you said so it is not one byte in each row it 's 8 byte.
as you said this is what your file is look like when you open it with notepad:

file.txt
01011001
00011001

right?
so this is 2 line and each line has 8 byte stored(line 1 has \n too).
if it is so,then you should read one line and write a routin to convert binary to decimal and print it..
search forum you can find out how to convert binary to decimal.
Last edited on
Thx for answering!
Yes, I see your point. it depends on how you read it, to me it is a byte, to the computer (it does not know that the text is written with only 1's and 0's) it is 8 byte (exluding the \n which is of no interest since I use getline)
Right now I am thinking of solving it the following way:

getline (myfile,line);
for (i=0;i:7;i++)
{
x=x+line[i]*2^(7-i)
}
cout << x << endl;

This will probably not work since line[i] is not an integer but I will solve that later. Coming from matlab one gets very lazy :P
Coming from matlab one gets very lazy :P

me too(coming from matlab)

what are you trying to do that in matlab you didnt need these thing?
I'm trying to establish a relation between three bytes (let's call them a,b and c): c=f(a,b)
for this task and with the bit operations that are done I think matlab will take to long and get too complex. I hope I understood your question correctly.
matlab is a very powerfull tool espesialy in matrix operations and image/signal proccessing and i used to use matlab but you know i didnt feel like a real programmer when i worked with it.and another thing is we cant access to matlab's frum from my country and many other problam so i migrated to c/c++.

The following for loop will start at the end of the string (notice i=strlen(line)-1) which is the low bit and for each char in the string that is 1 it will or in the value of the corrosponding bit (bitVal) to result x. bitVal is shifted left for each iteration of the for loop. This should work for a binary string up to 31 bytes in length.

1
2
3
4
5
6
7
8
9
int x=0;
int bitVal=1;

for( int i = strlen(line) - 1; i >= 0; i--, bitVal <<= 1 )
  x |= line[i] == '1' ? bitVal : 0;
	
cout << "binary " << line << endl;
cout << "dec    " << x << endl;
cout << "hex    " << showbase << hex << x << endl;
Topic archived. No new replies allowed.