所以我要接收一条消息(msg)并使用十进制基数将其转换为所有数字(A = 65,B = 66等)
到目前为止,我将消息保存为字符串,并尝试使用字符串流将其转换为十进制基数。这是做这件事的正确方法还是有更简单/更有效的方法?
这是我所拥有的:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string msg;
int P;
cout << "Enter a something: ";
cin >> P;
cout << "Enter your message: ";
cin.ignore( 256, '\n');
getline( cin, msg );
cout << endl << "Message Reads: " << msg << endl ;
int emsg; // To store converted string
stringstream stream; // To perform conversions
stream << msg ; // Load the string
stream >> dec >> emsg; // Extract the integer
cout << "Integer value: " << emsg << endl;
stream.str(""); // Empty the contents
stream.clear(); // Empty the bit flags
return 0;
}
示例运行:
Enter a something: 3 // This is used just to make things go smoothly
Enter your message: This is a message // The message I would like converted to decimal base
Message Reads: This is a message // The ascii message as typed above
Integer value: 0 // I would ultimately like this to be the decimal base message( Ex: 84104105 ...etc.)