binary to ascii, ascii to binary.

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

char bin_to_ascii(string bin);
string ascii_to_bin(char ch);


int main()
{
while(true)
{
string choice;
string str;
cout<<"Do you want to encrypt or decrypt the message: ";
cin>>choice;
if(choice == "encrypt")
{
char ch;
cout<<"Enter the string to encrypt:";
cin>> choice;
choice = ascii_to_bin(ch);
}
else if(choice == "decrypt")
{
cout<<"Enter the string to decrypt:";
string bin;
cout<<"Enter the string to encrypt:";
cin>> choice;
choice = bin_to_ascii(bin);
}
else
{ cout<<"Invalid Input\n\n"; }
cout<<"Repeat again(y or n)? ";
char ch;
cin>>ch;
if(ch != 'y' && ch != 'Y')
break;
}
}
char bin_to_ascii(string bin)
{
}
string ascii_to_bin(char ch)
{
}
hello guys, I am trying to convert from binary to ascii and vice versa, but i am somehwat stuck and not sure what to do . need help
I'm not sure exactly what you would like to do. My understanding is that you'd like to take a string such as "cat" and turn it into a string of 0s and 1s, such as "011000110110000101110100"

or a longer example, "Hello World!" becomes
0100100001100101011011000110110001101111
0010000001010111011011110111001001101100
0110010000100001

If that's the case, then both the encrypt and decrypt functions will take a string as a parameter and return a string result. If this is what you are aiming to do, then your two main functions might have a prototype like this:

1
2
string bin_to_ascii(string bin);
string ascii_to_bin(string ascii);


The encrypt function would need to process one character at a time, and convert that into a string of 8 binary digits. Each section would be added to the result string.

The decrypt function would do the reverse, it would take a substring of 8 characters at a time, and convert that binary string into a single ascii character. Each character would be added to the result string.

Also you would then probably use two additional functions to convert a single character. Those could be written using the built-in capabilities of the C++ std::bitset. Needs #include <bitset>

1
2
3
4
5
6
7
8
9
10
11
12
13
// convert a group of 8 digits to a character
char str_to_char(string group)
{
    bitset<8> temp(group);
    return temp.to_ulong();
}

// convert a character to a group of eight digits
string char_to_str(char ch)
{
    bitset<8> temp(ch);
    return temp.to_string();
}

one algorithm to go to-binary is (for 1 - byte ascii chars):
1) make a lookup table of the 4 bit binary numbers (0-15)
2)convert the integer value to hex using int to string tools in the language.
3) look up the hex digits into the table from 1
4) concat the results of 3 together.

converting back can be done with a map of the 4-bit strings, but a similar reversed approach.


#include<iostream>
#include<cstring>
#include<cmath>
#include <bitset>
using namespace std;

char bin_to_ascii(string bin);
string ascii_to_bin(char ch);


int main()
{
while(true)
{
string choice;
string str;
cout<<"Do you want to encrypt or decrypt the message: ";
cin>>choice;
if(choice == "encrypt")
{
char ch;
cout<<"Enter the string to encrypt:";
cin>> choice;
choice = ascii_to_bin(ch);
}
else if(choice == "decrypt")
{
cout<<"Enter the string to decrypt:";
string bin;
cin>> choice;
choice = bin_to_ascii(bin);
}
else{ cout<<"Invalid Input\n\n"; }
cout<<"Repeat again(y or n)? ";
char ch;
cin>>ch;
if(ch != 'y' && ch != 'Y')
break;
}
return 0;
}

char bin_to_ascii(string bin)
{

bitset<8> temp(group);
return temp.to_ulong();
}
string ascii_to_bin(char ch)
{
bitset<8> temp(ch);
return temp.to_string();
}

I have added in the code in, but there seem to be some error like group not declared. Even after i declare the group, some how it would not convert.
My goal is to convert 011000110110000101110100 into an ascii vice versa. please advice thanks!
@programnick I think you missed what I was saying.
My goal is to convert 011000110110000101110100 into an ascii vice versa. please advice thanks!

"011000110110000101110100" is a string. the result in ASCII is "cat" which is also a string.

Therefore your functions need to have these prototypes (or similar)
1
2
string bin_to_ascii(string bin);
string ascii_to_bin(string ascii);


Also, your code is rather muddled:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    while(true)
    {
        string choice;
        string str;
        cout<<"Do you want to encrypt or decrypt the message: ";
        cin>>choice;
        if(choice == "encrypt")
        {
            char ch;
            cout<<"Enter the string to encrypt:";
            cin>> choice;
            choice = ascii_to_bin(ch);
        }
        else if(choice == "decrypt")
        {
            cout<<"Enter the string to decrypt:";
            string bin;
            cin>> choice;
            choice = bin_to_ascii(bin);
        }
        else{ cout<<"Invalid Input\n\n"; }
        cout<<"Repeat again(y or n)? ";
        char ch;
        cin>>ch;
        if(ch != 'y' && ch != 'Y')
        break;
    }

At line 11, cin>> choice; you get the user input as a string. But what do you pass to the function? It isn't the user input, but something else, you pass a single uninitialised character ch.

That part of the code might look like this:
1
2
3
4
5
            cout <<"Enter the string to encrypt:";
            string str;
            cin >> str;
            string bin = ascii_to_bin(str);
            cout << "binary = \n" << bin << '\n';

It would be better to keep the variable choice out of this part of the logic, it serves its purpose in handling the user choice, use some other variables for the ascii/binary strings.

Tackling a program like this is a matter of breaking the task down into smaller subtasks. In the main() function we are concerned only with strings. The individual characters can be handled by the other functions.

The other two functions I mentioned would be used at the lowest level of all. So we would have a hierarchy:
main() calls
    bin_to_ascii() calls
        str_to_char()
        
    ascii_to_bin() calls
        char_to_str()


Also, it would help if you used code tags, then I could refer directly to line numbers in your code without having to requote it.
[code]your code here[/code]
Last edited on
Registered users can post here. Sign in or register to post.