Recently, I am looking at QT development to summarize several methods for String Conversion.
There are many methods. Here is the most common method: char * is the most basic type. Other types can be converted to char * first, and then converted to the type.
Cstring-char *
Cstring CSTR = "This Is A cstring .";
Char * Ch = CSTR. getbuffer (CSTR. getlength () + 1 );
CSTR. releasebuffer ();
Tchar * tstr = _ T ("This Is A tchar * string .");
Cstring CSTR;
// Assign values directly
CSTR = tstr;
// Or use the format Function
CSTR. Format (_ T ("% s"), tstr );
String-char *
Const char * str1 = "this is a const char *.";
String STR (str1 );
String str1 = "this is a string .";
Char * str2 = const_cast <char *> (str1.c _ STR ());
Qstring-char *
Const char * STR = "this is a C string .";
Qstring qstr (qstring: fromlocal8bit (STR ));
Const char * str2 = qstr. tolocal8bit ();
Qstring-string
# Include <qstring>
# Include <vector>
# Include <iostream>
# Include <string>
Using namespace STD;
Qstring s2q (const string & S );
String q2s (const qstring & S );
Qstring s2q (const string & S)
{
Return qstring (qstring: fromlocal8bit (S. c_str ()));
}
String q2s (const qstring & S)
{
Return string (const char *) S. tolocal8bit ());
}