Solved

how to convert from std::string to char

Posted on 2014-11-15
5
255 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 230
How to gracefully close the c++ 11 thread? 3 108
Path to  STL Map header file 1 70
Why isn't object file created? 6 69
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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