将句子转换为C 中的ASCII代码

convert a sentence to ascii code in c++

本文关键字:中的 ASCII 代码 句子 转换      更新时间:2023-10-16

我想将像'好男孩'这样的句子转换为ASCII代码。我知道循环的代码并打印每个句子每个字符的ASCII代码,但我不想要这个。我希望句子中的ASCII代码(所有字符alltoderth)为例1259788712 ..

您可以使用字符串来处理它。

#include <iostream>
#include <sstream>  // use stringstream
using namespace std;
// turn int into string
string IntTOstring(int);
int main(void)
{
    string sIn,sOut;
    // input 
    sIn = "good boy";
    sOut="";
    for (int i=0 ; i<sIn.length() ; i++ ) {
        // get one char from sIn each time
        int temp=sIn.c_str()[i];
        // turn int into string & save in sOut
        sOut += IntTOstring(temp);
    }
    cout << sOut << endl;
    return 0;
}
// use stringstream to convert int to string
string IntTOstring(int i){
    stringstream ss;
    ss << i;
    return ss.str();
}