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--; 
    } 
}

Ok, so I've actually pretty much figured out everything including the key, which is 17.

So I have all my integers converted to the correct decoded integers, but now I need to convert them to the ASCII which should read "THECAESARCYPHERISNOTVERYSECURE"
from what my program returns so far:
20,8,5,3,1,5,19,1,18,3,25,16,8,5,18,9,19,14,15,20,22,5,18,25,19,5,3,21,18,5,

heres my updated code...maybe I should remove my 'alpha' array in my 'decodeCharacterString() function? Where do I go from there?

#include<iostream> 
#include<fstream>
#include<cmath>
#define ARRAY_SIZE 999999

//function prototypes
void decodeCharacterString(char [],int, int);
void binaryStringToInt(char [], int &);
void reverseString(char []);
using namespace std;

int main()
{
    char binary[ARRAY_SIZE];
    int number;
    int key = 17;
    
    ifstream inFile;
    inFile.open("message.txt", ios::in);
    
    if(inFile.is_open())
    {   
        while(inFile >> binary)
        {
            
            decodeCharacterString(binary, number, key);
        }
    }    
    else 
        cout << "File open error" << endl << endl;
        
     
    
    system("pause");
    return 0;   
}

void decodeCharacterString(char b[],int n, int k)
{

    binaryStringToInt(b, n);
    if(n < 18)
        n = n - k + 26;
    else
        n = n-k;
    cout << n << ",";     //TEST    GOOD TO HERE
    
   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'};
    
    
}
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--; 
    } 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.