1

So I'm to take a message (msg) and convert it to all numbers using the decimal base (A=65, B=66 etc.)

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.)
user2059300
  • 361
  • 1
  • 5
  • 17
  • Do you think this code works? It's just that the code doesn't seem to match the description above. But the description above is a little confusing. Perhaps you should give some sample input and expected output. Would make things clearer. Just say something like 'if the input is ABC I expect the output to be ...'. – john Sep 02 '13 at 09:28

2 Answers2

1

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.

If you have a compiler that have range-based for loops then simply do

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';
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Ok, I have it in ascii and want to convert it to decimal base, but this seemed to be in the right direction. So I gave it a shot, and it gave me the error about not having a range-based compiler (htest.cpp:18:23: error: range-based ‘for’ loops are not allowed in C++98 mode) Any suggestions? – user2059300 Sep 02 '13 at 09:39
  • @user2059300 [ASCII](http://www.asciitable.com/) is the numeric representation on most modern systems, and printing those numeric values will produce e.g. `65` for the character `'A'`. – Some programmer dude Sep 02 '13 at 09:41
  • @user2059300 As for the iteration problem, either tell the compiler to use C++11 functionality (for GCC/clang add the option `-std=c++11`), or use normal iterators as in my updated answer. – Some programmer dude Sep 02 '13 at 09:43
1

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.

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • Hey thanks for this, it was really useful, is there a way to tunnel that output into an integer?? (and on a sidenote, should I open that up as a new question?) – user2059300 Sep 02 '13 at 10:00
  • If I'm understanding you correctly then the problem is going to be that '84104105 ...' is going to be too big an integer for C++ to handle. In most implementations of C++ integers cannot be bigger than 4 billion (or so). If you want to convert string into integers like that then you are going to have to look for a big integer library. – john Sep 02 '13 at 10:04
  • Ok, thanks, I think I'll sleep on this one some then, all good to keep in mind. – user2059300 Sep 02 '13 at 10:10
  • It just happens to be the integer you want **if** your system uses ASCII. That's quite common, but there are other encodings out there. – Pete Becker Sep 02 '13 at 12:28