I'm writing a code that takes a 5-bit binary string from a file 'message.txt', converts it to integers. The encoded letters are assigned numbers starting with a=1, b=2, etc. So if the original letter is 'a' and the key(fixed number of spaces) is 2, the encoded letter is 'c' which is converted to 3, so the file will contain binary string 0011.
My job is to decode the message. There are only 26 possible keys(0-25).
I'm very confused on how to figure out the keys. I've already converted the binary to integers, but now I'm stuck. I have absolutely no clue where to go in my assigned decodeCharacterString() function.
My code is a little sloppy, but its still in test phase, so please bare with me. I attached the 'message.txt' file and also the complete problem (problem.txt). Please help!
#include<iostream>
#include<fstream>
#include<cmath>
#define ARRAY_SIZE 999999
void decodeCharacterString(char []);
void binaryStringToInt(char [], int &);
void reverseString(char []);
using namespace std;
int main()
{
char binary[ARRAY_SIZE];
int number;
//numArray[ARRAY_SIZE]
ifstream inFile;
inFile.open("message.txt", ios::in);
if(inFile.is_open())
{
while(inFile >> binary)
{
binaryStringToInt(binary, number);
cout << number << ","; //GOOD TO HERE
//decodeCharacterString(numArray);
}
}
else
cout << "File open error" << endl << endl;
system("pause");
return 0;
}
/*void decodeCharacterString(int n)
{
char alpha[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int n = 0;
while(n != '\0')
{
cout << b[n] << endl;
n++;
}
}*/
void binaryStringToInt(char bin[], int &num)
{
reverseString(bin); //reverse string to start with at right of binary representation
int total=0,
exp = 0,
dec = 0,
n = strlen(bin);
for(int i = 0; i < n; i++)
{
if(bin[i] == '1')
num = 1;
else
num = 0;
dec = num * static_cast<int>(pow(2.0,exp));
total = total + dec;
exp++;
}
reverseString(bin); //reverse string again for output to main()
num = total; //total integer representation
}
void reverseString(char b[])
{
int n = strlen(b)-1;
for(int i = 0; i < n; i++)
{
char temp = b[i];
b[i] = b[n];
b[n] = temp;
n--;
}
}