Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Query as to how string to ascii conversion works?

string ascii

  • Please log in to reply
8 replies to this topic

#1 squid attack

squid attack

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 20 September 2010 - 06:43 PM

I am merely wondering as to HOW the following code works. Its all pretty much straight forward, but I am not seeing the actual 'mechanism' that converts the string to ascii numerals??
It must either happen in the 'for loop', or in the final cout statement, but where? Im not seeing the actual code that does the conversion.

string str; 
     cout<<"Enter string : ";  
     cin>> str;
    for(int a = 0; a!=str.length(); ++a) 
            cout<<int(str[a])<<", "; 

  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 20 September 2010 - 08:10 PM

All characters in that context are stored as ASCII codepoints (0-127) in memory, that is what your "char" will hold.

It is the same context as typecasting the char into an int, imagine the following:
std::cout << (char)'a'; //01100001 = 'a' in memory
std::cout << (int)'a'; //01100001 -> 97 in decimal
One will output the ASCII character 'a', the other the ASCII codepoint, as you are forcing implicit conversion to an int. Your code uses casting with a function, int('a').
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 squid attack

squid attack

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 20 September 2010 - 10:12 PM

Right. So its just the cout line of code int(str[a] thats doing the actual 'conversion' here.
  • 0

#4 squid attack

squid attack

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 21 September 2010 - 02:03 AM

Another question regarding the original code I posted.

The for loop in the original code iterates through the letters in the string and the (int) letter part converts each to ascii numerals, how is it possible to retain that ascii conversion as a string, ie; almost as a form of encryption?

ie; If I enter a string and str = "variouschars" and it converts each letter to asci numerals how could I retain that string as asci numerals.

I tried to put it into an array but failed dismally.
I tried also to store it in another string but that didnt want to work either.
I'm baffled?
  • 0

#5 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 21 September 2010 - 03:25 AM

Retaining ASCII in a concatinated string is a bit silly, so you can use an array for this, yes.

#include <iostream>
#include <string.h>

using namespace std;

int main() {
    char str[20];
    int result[20] = {0};
    cin.getline(str, 20);
    
    for(unsigned int a = 0; a != strlen(str); a++) 
        result[a] = (int)str[a]; //assign to array
    
    for(unsigned int a = 0; a != strlen(str); a++)
        cout << result[a] << ", "; //print array
}

By encryption, what do you mean? Storing as ASCII would be obfuscation not encryption, if you are looking to encrypt a result then you can look at the GNU crypt library.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 21 September 2010 - 03:27 AM

Save that string as integer array.
int array[] = {'s', 't', 'r', 'i', 'n', 'g'};
If you output this array you will get ASCII numbers for corresponding letters. Is that what you meant?
  • 0

The roots of education are bitter, but the fruit is sweet.


#7 squid attack

squid attack

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 21 September 2010 - 03:43 AM

Brilliant.
Thanks for showing the code. I now know what I was doing wrong when I was trying to put the ascii numerals into the array, (at first char* then int arrays, both wrong!)

I'm just giving myself projects to teach myself c++ and not really trying to encrypt anything, just playing with strings, but nonetheless the GNU crypt library mentioned sounds interesting.
Thanks for the help.
  • 0

#8 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 21 September 2010 - 04:09 AM

You can have strings saved in integer arrays, just cast them to char when you ouput them:
int intArray[] = {'s', 't', 'r', 'i', 'n', 'g'};
for (int i = 0; i < 5; ++i)
    std::cout << (char)intArray[i];    // this will output letters instead of numbers, because we casted them to char

And as Nullw0rm said, you can also have strings and display their ASCII values.
  • 0

The roots of education are bitter, but the fruit is sweet.


#9 squid attack

squid attack

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 21 September 2010 - 02:04 PM

Excellent. Thanks very much for the responses.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download