C++ 将字符串转换为 ASCII

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6709795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:34:12  来源:igfitidea点击:

Converting string to ASCII

c++ascii

提问by E.O.

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:

我只是在尝试一些东西并制作了以下代码。它应该采用字符串中的每个单独的字母并打印其 ASCII 等效项。但是,当有空格时,它会停止转换。这是代码:

#include <iostream>
#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?

关于为什么会发生这种情况的任何想法?

回答by Rob?

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

cin >> plainText从输入中读取,但不包括第一个空白字符。你可能想要std::getline(cin, plainText)

References:

参考:

回答by Sven

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

格式化的输入功能operator>>istream,如果它击中的空间从流将停止提取。所以你的字符串不包含输入的其余部分。

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

如果您希望阅读到行尾,请getline改用:

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

回答by Nirzak

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.

只需使用getline,然后不需要这些东西,您只需将字符串类型转换为int 字符串即可将其直接转换为ascii。这是我的代码。

#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;
}

回答by nelavara

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.

这是我放在一起的东西。我使用了一个向量来存储所有要生成的 ASCII 值。我们首先向用户询问一个字符串。然后我们使用类型转换并将值添加到向量中。我们还使用 while 循环来防止用户不输入任何内容。

# 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

在此处输入链接描述

回答by Vishal Bajaj

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.

使用这两行代码接受带有空格的字符串。