- Unicode字符集
CString转 char*
CSting str;
char* p;
USES_CONVERSION;
p=T2A(str);
char*转CString
char* p;
CString str;
USES_CONVERSION;
str=A2T(p);
CString转string
CSting str;
string s;
USES_CONVERSION;
s=T2A(str);
string转CString
string s;
CString str;
str=s.c_str();
string转char*
string s;
const char* p=s.c_str();
//或
char p[100];
strcpy(p,s.c_str());
//自定义函数实现string 转char*
char* string2charp(string str)
{
char* p;
const int len = str.length();
p = new char[len+1];
strcpy(p,str.c_str());
return p;
}
char*转string
char*p;
string s(p);
//或
string s;
s=p;
- 多字节字符集
CString转 char*
CSting str;
char *p;
p=str.GetBuffer(str.GetLength());
char*转CString
char* p;
CString str;
str=p;
CString转string
CSting str;
string s;
s=str.GetBuffer(0);
string转CString
string s;
CString str;
str=s.c_str();
//或
str.Format("%s",s.c_str());
string转char*
*同unicode
char*转string
*同unicode
UTF8转UNICODE
wstring DecodeUtf8(string in)
{
wstring s(in.length(), _T(' '));
size_t len = ::MultiByteToWideChar(CP_UTF8, 0,in.c_str(), in.length(),&s[0], s.length());
s.resize(len);
return s;
}
UNICODE转UTF8
// 宽字符转UTF8
string EncodeUtf8(wstring in)
{
string s(in.length()*3+1,' ');
size_t len = ::WideCharToMultiByte(CP_UTF8, 0,in.c_str(), in.length(),&s[0], s.length(),NULL, NULL);
s.resize(len);
return s;
}