Solved

how to convert from std::string to char

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

737 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