I am trying to convert a 16 bit input read from a file to 3 parts consisting of 4, 4 and 8 bits respectively.
eg. 1011010100111101 to 1011 0101 00111101
This parts are then supposed to be converted to a decimal value. I have to read the I have tried using substrings, atoi, stringstream and all the methods i can think of, but i still seem to be doing something wrong. Please note that i am only a beginner. My course is an introductory one and we have covered selection, iteration, functions, arrays, structures, strings and files.
My log file currently consists of 2 data
1011100110110111
1000001110010101
Please help.
//Program to demonstrate a wind speed proccesor
//reads data from a log file and processes it to show the day, direction and speed of the wind
//calculates the maximum, mimimum and average speed
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
int binarytoDecimal(string);
int main()
{
string inpStr,day, direction, speed;
int W_speed, W_direction, W_day;
//declare input file stream object to read the data
ifstream inStream;
//open the file
inStream.open("data.txt", ios::app);
//check if the file opened successfully
if (inStream.is_open())
{
while(! inStream.eof())
{
//read the data from the file
getline(inStream,inpStr);
day = inpStr.substr(0,4);
int atoi( const string *d );
W_day = binarytoDecimal(d);
getline(inStream,inpStr);
direction = inpStr.substr(5,8);
int atoi( const string *direction );
W_direction = binarytoDecimal(dir);
getline(inStream,inpStr);
speed= inpStr.substr(9,8);
int atoi( const string *wind );
W_speed = binarytoDecimal(wind);
cout<< day<<" "<< endl;
cout<< direction<<" " << endl;
cout<< speed<<" " << endl;
if(inStream.eof())
exit(1);
}
}
else //file open failed
{
cout << "File open error "<< endl;
}
return 0;
}
int binarytoDecimal(string inpStr)
{
int i ,res= 0 ;
double power = 0.0;
int digit;
for(i = inpStr.length() - 1; i >= 0; i--)
{
digit = inpStr[i] - '0';
res += (digit*pow(2.0,power ));
power ++;
}
return res;
}
The output im getting is
0
54
21

New Topic/Question
Reply



MultiQuote





|