-
type conversion
Hi,
why doesn't this work?
Code:
#include <iostream>
using namespace std;
int main()
{
string abc = "12345abcde";
char une;
int uno;
une=abc[1]; // une = "2"
uno=(int) une; // uno=2
uno *=3; // uno=6
cout<<"uno is: "<<uno<<endl;
//outputs "uno is 150"
return 0;
}
why does c++ find it so difficult to
reinterpret a character 2 from a string
as a numeral 2 ?!
thanks
-
The char type in C++ is just a small integer that holds character codes. So when you convert from char to int, you aren't converting from the numeric character to the number, you are converting from the numeric character's character code to a number (which isn't really a conversion at all, since they are both numbers).
To convert a single character into it's numeric equivalent, simply subtract the character for zero: '0'. This works because the characters for all 10 digits are guaranteed to be in order.
To convert a string to an int, you need to use a slightly more advanced method like a stringstream, atoi, or boost::lexical_cast.
-
Which one of those methods to convert a character within an instance of the string class to an integer is the most efficient Daved?
-
-
You should be asking which is the best to use design-wise. You might also ask if any have major efficiency issues if your program requires extra attention to speed or size.
As far as best for design goes, I generally prefer to use the stringstream because I know how to test it for success or failure and configure it to convert the way I want it to, plus I use it for int to string conversions as well so there is the benefit of familiarity and consistency in the code. I would use boost lexical_cast if I was already using other boost libraries in my code. I have used atoi many times before in simple test code or C style code written by others that I was maintaining.
Basically, any of the choices would work. I would guess atoi might be a tiny bit faster in practice, since it is lower level than the other two, but like I said that is rarely a valid concern even if it is true. I honestly don't know if any can be the cause of a major bottleneck, I've never noticed one.
BTW, don't forget to #include <string>.
-
Thanks Daved,
however,
Code:
une=abc[1]; // une = "2"
uno=atoi(une);
does not compile and gives the message
"invalid type conversion from char to const char*.
Also, how do I subtract 0 from 2 as you suggest?
Thanks
-
Just subtract '0' with the single quotes. A character within single quotes is treated as a character in the the code. So '0' is the character that represents zero, but 0 is the actual number 0. So to subtract the character zero, just subtract '0'.
Code:
uno = une - '0';// uno = 2
The atoi function works with a C style string, which is an array of characters. That is why it didn't work when you passed in just the char variable. To get a C style character array from a string variable like abc, you would use the function c_str(). So with this code:
Code:
uno = atoi(abc.c_str()); // uno = 12345
uno would be 12,345 because it converts the whole string to a number.
You can use atoi if you want to convert the entire number at once, and use the subtract '0' technique if you want just a single digit.
-
ah ha I see, so 0 being ASCII 48 and 1,2,3 being 49,50,51 you just subtract 48 to convert from ASCII to fingers and toes. very good.
atoi seems to be working now too. thanks a lot.