Solved

how to convert from std::string to char

Posted on 2014-11-15
5
251 Views
Last Modified: 2014-11-22
How could I take a std::string containing a number like "65" and want to convert that into a char value, in this case 'A'?
0
Comment
Question by:rustycp
5 Comments
 
LVL 2

Author Comment

by:rustycp
ID: 40444918
This is what I did but if there's a cleaner better way I'd still like to know:

string str = "123";
char v = (char)(boost::lexical_cast<int>(str));
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40444924
The simplest way would be a 'stringstream', e.g. like

#include <string>
#include <sstream>
using namespace std;

//...

int n;
char c;
string str = "123";
stringstream ss(str);

n << ss;

c = (char)n;

Open in new window


Using the operators '>>' and '<<', you can pretty much convert anything to other types using stringstreams, given that these overators were overloaded for the respective types.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40444985
Usually you need to include a catch of boost::bad_lexical_cast .
You are making assumptions about the size of the integer value represented by the string. What if it is a larger than a char? Do you care?

In case you want to use other conversion functions such as atoi, then std::string::c_str() is used to return a const pointer to the c-style string.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40445001
Under the hood in the streams used to convert strings to int is a memory allocation, which is why this approach, while providing safety, is slower than other approaches.

BTW, should it be
ss >> n;
?

Take away safety aspects, and you can even use sprintf, atoi, strtol.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 40449613
Take away safety aspects, and you can even use sprintf, atoi, strtol.
atoi and strtol are safe. atoi never fails but will stop at the first non-digit. strtol also doesn't fail but tells where it stopped conversion, what allows to detect wrong input. sprintf(s, "%d", intvar); is not safe cause the format string "%d" could be changed to "%s" what could be used to produce a buffer overflow if an attacker also changed the value of 'intvar' to a valid pointer value. however, the attacker must use a debugger (or similar) to do so.

in c++ string streams as suggested by jkr are the preferred way (when using the correction 's >> n;' as told by phoffric). the string stream operator>> is also safe regardless of the contents cause it will fail if the string value is not numeric or beyond integer size but would not throw an exception. also the cast to char is safe as it can handle all possible int values even if they are beyond char size. but of course values greater than 127 or negative integer values less than -128 would be wrong.

Sara
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fully specialized class template function 21 136
I could not build boost code, 10 89
White board coding practice 3 88
C++ mouse_event mouse look 7 84
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question