简体   繁体   English

将字符串转换为十进制基数C ++

[英]Convert a string to a decimal base C++

So I'm to take a message (msg) and convert it to all numbers using the decimal base (A=65, B=66 etc.) 因此,我要接收一条消息(msg),并使用十进制基数将其转换为所有数字(A = 65,B = 66等)

So far, I took the message and have it saved as a string, and am trying to convert it to the decimal base by using a string stream. 到目前为止,我接受了消息并将其另存为字符串,并尝试通过使用字符串流将其转换为十进制基数。 Is this the right way to go about doing this or is there an easier/more efficient way? 这是执行此操作的正确方法,还是有更简单/有效的方法?

Here is what I have: 这是我所拥有的:

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

Example Run: 示例运行:

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.)

If you want to convert every character in the string to its ASCII equivalent (which seems to be what you want) then you have to iterate over the string and simply take each characters as a number. 如果要将字符串中的每个字符转换为等效的ASCII码(这似乎是您想要的),则必须遍历字符串并将每个字符简单地当作一个数字。

If you have a compiler that have range-based for loops then simply do 如果您的编译器具有基于范围的for循环,则只需执行

for (const char& ch : msg)
{
    std::cout << "Character '" << ch << "' is the same as "
              << static_cast<int>(ch) << '\n';
}

If you have an older compiler, then use normal iterators: 如果您使用的是较旧的编译器,请使用普通的迭代器:

for (std::string::const_iterator itr = msg.begin();
     itr != msg.end();
     ++itr)
{
    std::cout << "Character '" << *itr << "' is the same as "
              << static_cast<int>(*itr) << '\n';
}

You don't need to use stringstream, its much easier than that, just cast to unsigned char (in case you have any chars with a negative encoding) and then to int. 您不需要使用stringstream,它比这要容易得多,只需将其转换为无符号字符(如果您有任何带有负编码的字符),然后再转换为int。

cout << "Integer value: ";
for (size_t i = 0 ; i < msg.size(); ++i)
    cout << static_cast<int>(static_cast<unsigned char>(msg[i]));
cout << "\n";

Every character is encoded by an integer, which just happens to be the integer you want. 每个字符都由一个整数编码,该整数恰好是您想要的整数。 So you can do the conversion with a simple cast. 因此,您可以使用简单的转换进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM