?
Solved

how to convert from std::string to char

Posted on 2014-11-15
5
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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 35

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

765 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