Solved

how to convert from std::string to char

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now