Jump to content

Query as to how string to ascii conversion works?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
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])<<", "; 

 


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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').
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
squid attack

squid attack

    Learning Programmer

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

#4
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
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?

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
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.

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
squid attack

squid attack

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Excellent. Thanks very much for the responses.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users