Conversion of 1.string to int
1) in C Standard library, use Atoi:
#include <cstdlib>
#include <string>
std::string Text = "152";
int number = Std::atoi (Text.c_str ());
if (errno = = erange)//May be Std::errno
{
Number may not be fully stored because it is too large or too small
}
else if (errno = =????)
It could be einval.
{
cannot be converted into a number
}
2) in the C + + standard library, use StringStream: (StringStream can be used for conversions between various data types)
#include <sstream>
#include <string>
std::string Text = "152";
int number;
Std::stringstream SS;
SS << text;//can be other data types
SS >> number; string-int
if (! Ss.good ())
{
Error occurred
}
SS << number;//int->string
String str = SS.STR ();
if (! Ss.good ())
{
Error occurred
}
3) in the Boost library, use Lexical_cast:
#include <boost/lexical_cast.hpp>
#include <string>
Try
{
std::string Text = "152";
int number = boost::lexical_cast< int > (text);
}
catch (const Boost::bad_lexical_cast &)
{
Conversion failed
}
2. String Transfer CString
Cstring.format ("%s", String.c_str ());
Using C_str () is really better than data ();
3.char Turn CString
Cstring.format ("%s", char*);
4.char Turn string
string S (char *);
Can only be initialized, preferably in a place that is not initialized or with assign ().
5.string Turn char *
Char *p = STRING.C_STR ();
6.CString Turn string
String s (Cstring.getbuffer ());
GetBuffer () must be releasebuffer (), otherwise the space occupied by the buffer will not be released.
7. The contents of the string are converted to character arrays and c-string
(1) data (), returns an array of strings without "\"
(2) C_str (), returns a string array with "\"
(3) Copy ()
Conversion between 8.CString and int, char*, char[100]
(1) CString Mutual Transfer int
To convert a character to an integer, you can use Atoi, _atoi64, or ATOL. Instead of converting a number to a CString variable, you can use the CString format function. Such as
CString s;
int i = 64;
S.format ("%d", i)
The Format function is very powerful and deserves your research.
void Cstrdlg::onbutton1 ()
{
CString
Ss= "1212.12″;
int Temp=atoi (ss);
CString AA;
Aa. Format ("%d", temp);
AfxMessageBox ("var is" + AA);
}
(2) CString Mutual transfer char*
char * to CString
CString strtest;
char * CHARPOINT;
Charpoint= "Give string a value"; //?
Strtest=charpoint;
CString to char *
Charpoint=strtest. GetBuffer (strtest. GetLength ());
(3) Standard C does not have String,char *==char []==string, you can use the Cstring.format ("%s", char *) method to convert char * to CString.
To turn CString into char *, use the operator (LPCSTR) to CString.
CString conversion char[100]
Char a[100];
CString Str ("aaaaaa");
strncpy (A, (LPCTSTR) str,sizeof (a));
BTW: Use the Atof () function to transfer the string type char* float to floating point type
Function prototype: Double atof (const char *nptr);
Function Description: Atof () scans the parameter nptr string, skips the preceding space character until it encounters a number or sign to start the conversion, and then encounters a non-numeric or string ending (") to end the conversion and return the result. The parameter nptr string can contain a positive sign, a decimal point, or E (e) to represent an exponential portion, such as 123.456 or 123e-2. The return value returns the number of converted floating-point types.
Example:
/* Convert string A and string B to numbers and add */
#include <stdlib.h>
Main ()
{
Char *a= "-100.23";
Char *b= "200e-2";
float C;
C=atof (a) +atof (b);
printf ("c=%.2f\n", c);
}
Cross-conversion of string, char*, int types in C + +