If I have this below CString data and I want to change it format to int.
Example:
CString data_str = "1487";
int data_int;
I want
data_int = 1487
Do you know how to do that?
If I have this below CString data and I want to change it format to int.
Example:
CString data_str = "1487";
int data_int;
I want
data_int = 1487
Do you know how to do that?
Just use one of the standard C funcitons like: sscanf, atoi, strtol...
CString data_str = "1487";
int data_int = atoi(data_str);
One way, but beware that the std lib interpretations are not Unicode aware.....so as the rest of MFC can switch between Unicode & ANSI easilly, all your code should be able to do this too.Originally posted by Monster
Just use one of the standard C funcitons like: sscanf, atoi, strtol...
CString data_str = "1487";
int data_int = atoi(data_str);
Try _ttoi which is defined in tchar.h (part of a normal MFC build nayway, so no extra headers).....
If its unicode, _ttoi goes to _wtoi (M$'s Unicode version of atoi). If not, then plain old atoi is used
Code:int x = 1487;//Pick an int CString data_str;//Creat a string data_str.Format(_T("%d"),x);//make int into string MessageBox(data_str);//display int y = _ttoi(data_str);//string to int y += 1000;//change int data_str.Format(_T("%d"),y);//back to string MessageBox(data_str);//display
How does CString::getBuffer() work for this? Doesn't it return a const char* to the char array? If I shouldn't be using it why not?
Couldn't think of anything interesting, cool or funny - sorry.
I believe atoi() and related functions require null terminated array/Cstyle string and will not work with instances of string classes.
Most string classes have some function to return the underlying null terminated char array/Cstyle string. In some classes it's c_str() but I don't use CStrings so I can't say for sure what it is there. In fact many classes have methods that convert the string to an int for you. I know of several classes where it it ToInt(), but again I don't use CString so I can't say whether there is such a conversion function or not. The syntax
Cstring sInput = "1234";
int iInput = atoi(sInput.c_str());//or ttoi() or whatever
iInput = sInput.ToInt();
Very true in std::string....but not so in CString....there is a conversion operator that allows implicit conversion to LPTSTR or whatever.....Originally posted by elad
I believe atoi() and related functions require null terminated array/Cstyle string and will not work with instances of string classes.
Most string classes have some function to return the underlying null terminated char array/Cstyle string. In some classes it's c_str() but I don't use CStrings so I can't say for sure what it is there. In fact many classes have methods that convert the string to an int for you. I know of several classes where it it ToInt(), but again I don't use CString so I can't say whether there is such a conversion function or not. The syntax
Cstring sInput = "1234";
int iInput = atoi(sInput.c_str());//or ttoi() or whatever
iInput = sInput.ToInt();
Not a very good practice as far as I know as implicit conversions can lead to problems, and according to my C++ texts, that's why the std::string doesnt support this facility...the great and the good decided that it was far to error prone and so std::string::c_str() was brought in to allow explicit conversion