6 Replies - 3256 Views - Last Post: 01 June 2011 - 11:27 AM Rate Topic: -----

#1 jsalehi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 11

Having problem in converting the ascii code back to the strings.

Posted 01 June 2011 - 06:11 AM

I really have no idea how to do this, Im studying biotechnology and I have this programming for second year so please a great deal of explanation will help a lot. And I wasa using this code to understand something from I got nothing.
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str; //String                                                        
    int a; //Number of letters                                                  

    cout<<"Enter string : ";   /*Inputs the characters*/
    cin>>str;

    for(a = 0; a!=str.length(); ++a) /*Prints out each letter converted into a \
int value.*/
        cout<<int(str[a])<<", ";
    cout<<endl;
    cin.get();
    return 0;

}


Is This A Good Question/Topic? 0
  • +

Replies To: Having problem in converting the ascii code back to the strings.

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Having problem in converting the ascii code back to the strings.

Posted 01 June 2011 - 06:26 AM

Can you please be a bit more verbose about what exactly you're trying to do? (I see very little correlation of the topic with the source code)
Was This Post Helpful? 0
  • +
  • -

#3 jsalehi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 11

Re: Having problem in converting the ascii code back to the strings.

Posted 01 June 2011 - 06:32 AM

i have this assignment. Where i have to take a string from a user and encrypt or decrypt it and then print it back as a string. But i was able to do it without using strings.
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
        int number;
        char letter;
        int option,y,i=0;
		int rotor1[20] = {59,88,28,31,49,69,58,15,45,81,20,57,46,25,87,3,4,1,11,83};
int rotor2[20] = {83,41,66,65,96,2,8,93,40,70,55,24,90,95,34,89,98,24,44,85};
int rotor3[20] = {58,91,18,60,36,54,100,90,47,50,12,52,62,78,57,18,35,36,33,43};



do{
        cout<<"Choose 1 to decrypt and choose 2 to encrypt the messege";
        cout<<endl<<"[1] * Decryption"
                <<endl<<"[2] * Encryption"
                <<endl<<"[3] * EXIT"<<endl;;
        
        cin>>option;
        switch (option) //Detects the option
               {
                case 1:
					{cout<<"Enter a letter : ";
                        cin >> letter; //Inputs the letter
                        cout<<"The character you entered is : \""<<int(letter)<<"\" in ASCII"<<endl; //Outputs the same letter in int
                      

				y=letter-rotor1[i];
		                if (y < 32)
							y = 126-(32-y);

						else if (y > 126)
							y = (y-126)+32;
						y=y+rotor2[i];
						if (y < 32)
							y = 126-(32-y);
						else if (y > 126)
						y = (y-126)+32;
						y=y+rotor3[i];
						if (y < 32)
							y = 126-(32-y);
						else if(y > 126)
							y = (y-126)+32;
						
							printf("%d",y);
							cout<<"The number you entered is : \""<<char(y)<<"\" in ASCII"<<endl; //Ouputs the same number in char
							i++;
                        break;
		}

                case 2:
					{               cout<<"Enter a letter : ";
                        cin >> letter; //Inputs the letter
                        cout<<"The character you entered is : \""<<int(letter)<<"\" in ASCII"<<endl; //Outputs the same letter in int
						

				y=letter+rotor1[i];
		if (y < 32)
							y = 126-(32-y);

						else if (y > 126)
									y = (y-126)+32;
						y=y+rotor2[i];
						if (y < 32)
							y = 126-(32-y);
						else if (y > 126)
							y = (y-126)+32;
							y=y+rotor3[i];
						if (y < 32)
							y = 126-(32-y);
						else if(y > 126)
							y = (y-126)+32;

							printf("%d",y);
							cout<<"The number you entered is : \""<<char(y)<<"\" in ASCII"<<endl; //Ouputs the same number in char
                        i++;
                        break;
					}
                case 3:
					{   return 0;}

                default: {//If user chooses anything else besides options given
                        cout<<"Invalid Option!";
                        system("PAUSE");
                        break;
		}}
}while (option !=3);

        system("PAUSE");
        return 0;
}


But how will i convert strings to there ascii codes and then back to strings. Thats my problem.
Was This Post Helpful? 0
  • +
  • -

#4 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Having problem in converting the ascii code back to the strings.

Posted 01 June 2011 - 06:50 AM

You don't really have to "convert" ASCII code to char. Therefore I'm having a hard time in understanding what you want. ;)

#include <iostream>
#include <string>

int main() {
    using std::string;
    using std::cout;
    
    string s;
    s.push_back(99);
    s.push_back(102);
    
    cout << "value: [" << s << "]\n";
    
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 jsalehi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-May 11

Re: Having problem in converting the ascii code back to the strings.

Posted 01 June 2011 - 07:16 AM

Fine, can you tell me how to convert strings to ascii and then back
Was This Post Helpful? 0
  • +
  • -

#6 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Having problem in converting the ascii code back to the strings.

Posted 01 June 2011 - 08:17 AM

You seem to not grasp what strings are - on the most basic level, they are just an arrays of numbers that represent characters.
char is a data type, it's pretty much integer that usually takes 1 byte of space. You'll find out that a byte is usually 8 bit long, so you can have 256 values in 1 byte. That means it can hold numbers between 0..255 or -128..127 (which one depends on the implementation, but it's more common to be the latter).
So, a char that has a value of 99 is 'c'. It already is - you don't have to magically convert 99 to 'c' - they are one and the same value. ;)
You will have to cast to other type to change it's text representation however, since char value will be treated as a character, and if you cast it to int - it'll be treated as a number. :)

Now, you can clearly see how you can get a string, save it as a series of numbers in text form and then read the numbers back and make a string out of them:

#include <iostream>
#include <string>
#include <sstream>

int main() {
    using namespace std;

    string msg = "hello there";
    for (string::size_type i = 0; i < msg.size(); ++i) {
        cout << static_cast<int>(msg[i]) << ' '; 
    }
    cout << '\n'; 
    // this will print: 104 101 108 108 111 32 116 104 101 114 101 

    
    int ascii_value = 0;
    string new_msg = "";
    stringstream ss("104 101 108 108 111 32 116 104 101 114 101"); // string buffer
    while (ss >> ascii_value) { // reading integer numbers from it
        new_msg.push_back(ascii_value); // putting the numbers into a string
    }
    cout << new_msg << '\n'; // will print hello there
    
    
    return 0;
}

This post has been edited by Xupicor: 01 June 2011 - 08:19 AM

Was This Post Helpful? 1
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Having problem in converting the ascii code back to the strings.

Posted 01 June 2011 - 11:27 AM

First off lets learn a little bit about ASCII and strings.

A char is a numeric type that typically occupies 1 byte and can have 256 different values (for a signed char that is -128 to 127, for an unsigned char that is 0 to 255). It is very important to remember that chars are NUMBERS and not magically letters like A, B, C.

To represent letters and other symbols we need to make a map from number to the character we want it to represent. Typically the ASCII codes are used as a basic mapping. ASCII is not the only mapping but it is kind of "built-in" to C/C++ so it often gets used.

So in C/C++ a char 'A' is the same as the number 65. You can write:

cout << 10 +'A' << endl;

and get 75 as a result because the char 'A' is just a number.

so:
#include <iostream>
#include <string>

using namespace std;

int main() {
    char myStr[] = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0};
    
    cout << myStr << endl;
    
    string str(myStr);
    
    for(int i = 0; i < str.length(); ++i) {
        cout << (int)str[i] << ",";
    }
    cout << endl;
    
    int asciiValues[] = { 83, 101, 101, 32, 104, 111, 119, 32, 119, 101, 108, 108, 32, 116, 104, 105, 115, 32, 119, 111, 114, 107, 115, 46 };
    string theString;
    
    for (int i = 0; i < sizeof(asciiValues)/sizeof(int); ++i) {
        theString.push_back( asciiValues[i] );
    }
    
    
    cout <<  theString << endl;
    
    for(int i = 0; i < theString.length(); ++i) {
        cout << (int)theString[i] << ",";
    }
    cout << endl;

    return 0;
}




So there is no need to "convert" between a char and ASCII... you may want to convert between and int and a char to make it explicit that you mean "number" and not "character"


So for example if I write this:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1