开发者

Converting string to ASCII

I was just trying something out and made the following code. It is supposed to take each individual letter in a string and print its ASCII equivalent. However, when there is a space, it stops converting. Here is the code:

#include <io开发者_C百科stream>
#include <string>
using namespace std;

void convertToASCII(string letter)
{
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        cout << int(x) << endl;
    }
}

int main()
{
    string plainText;
    cout << "Enter text to convert to ASCII: ";
    cin >> plainText;
    convertToASCII(plainText);
    return 0;
}

Any ideas on why this happens?


cin >> plainText reads from the input up to, but excluding, the first whitespace character. You probably want std::getline(cin, plainText) instead.

References:

  • std::getline
  • istream& operator>> (istream& is, string& str)


The formatted input function operator>> on istream stops extraction from the stream if it hits a space. So your string doesn't contain the rest of the input.

If you wish to read until the end of the line, use getline instead:

string plainText;
cout << "Enter text to convert to ASCII: ";
getline(cin, plainText);
convertToASCII(plainText);


Just Use getline and then no need such things you can just typecast to int a string letter to directly convert it to ascii.Here Is My Code.

#include <iostream>
#include <string>
using namespace std;

void convertToASCII(string s)
{
    for (int i = 0; i < s.length(); i++)
    {
        cout << (int)s[i]<< endl;
    }
}

int main()
{
    string plainText;
    cout << "Enter text to convert to ASCII: ";
    getline(cin,plainText);
    convertToASCII(plainText);
    return 0;
}


Here is something I put together. I used a vector to store all of the ASCII values that are to be generated. We first ask the user for a string. Then we use type casting and add the values to a vector. We also use a while loop to prevent the user from not entering nothing.

# include <iostream>
# include <string>
# include <vector>

std::vector<int> converttoASCII (std::string s)  //used a vector to store all our ASCII values
{
    std::vector <int> vals;  //vectpr creation
    int ascChar;
    for (int i = 0; i < s.length(); i++)  //We interate through string passed and add to vectors
    {
        ascChar = s[i];
        vals.push_back(ascChar);
    }
    return vals;
}


int main()
{
    std::string toencode;
    std::cout << "Please enter in a string to encode: ";
    std::getline(std::cin, toencode);
    while (toencode.length() == 0)  //we used a for loop to prevent user from entering nothing.
    {
        std::cin.clear();
        std::cout << "Must not be empty! Try Again.\n";
        std::cout << "Please enter in a string to encode: ";
        std::getline(std::cin, toencode);
    }
    std::vector <int> asciivals = converttoASCII(toencode);
    for (int i : asciivals)  //Print out the results of the vector
    {
        std::cout << i << "\n";
    }
    return 0;
}

References:

enter link description here

enter link description here

enter link description here


Simple and easy code

string mystring= "ABC DEF ++ -- ";
for (char c : mystring) cout << (int)c << endl;

This code will check characters one by one and output equivalent ascii value

Output: 65 66 67 32 68 69 70 32 43 43 32 45 45 32


cin.ignore();
cin.getline(plaintext,100); // 100 (assumed) is the size of plaintext

Use these two lines of code to accept a string with blank spaces.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜