Hi. I wrote a binary to character converter program but I have a problem - after I type in the binary (e.g. 01000001) the program stops with an error. I ran it in win XP and linux but both give error.
The errors:
XP:
F:\>bin2char-0.1
Enter string to convert : 01000001
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
and linux:
[dvldrmmr@myhost /]# ./bin2char-0.1
Enter string to convert : 01000001
Aborted
Any ideas why I get an error? I wrote an *almost* identical app in VB (basic vs. c++) and it worked fine.
My code (I know its not too pretty ):
Code:
/***********************************************************/
/* bin2char-0.1.cpp - Binary to Character Converter v. 0.1 */
/***********************************************************/
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{ string bin_stream, char_stream;
int ascii_num, char_start;
int count = 0;
char ch;
cout << "Enter string to convert : ";
cin >> bin_stream;
if(bin_stream.length() % 8 != 0){
cout << "Incorrect input string!!\n";
return 0; }
for(int i = bin_stream.length(); i > 0; i - 8){
for(int j = 0; j <= 7; j++){
char_start = count * j + 1;
ch = bin_stream.at(char_start + j);
if(ch == '1'){ ascii_num += (int)(pow((double)2,j)); }
}
count++;
char_stream += (char)ascii_num;
}
cout << "Converted string:\n" << char_stream << endl;
return 0;
}
Is it my code?
Any help appreciated. Thanks.