目录
在mfc中,char* ,CString,string之间的转换是使用非常频繁的,这里参考了网上的许多方法,这里做一个总结
void CString_to_chars()
{
char ch[256];
CString cs = _T("this is test CString_to_chars");
#ifndef UNICODE
int str_len = cs.GetLength();
char *buf = cs.GetBuffer(str_len);
memcpy(ch, buf, str_len);
ch[str_len] = '\0';
cs.ReleaseBuffer();
#else
int len = WideCharToMultiByte(CP_ACP, 0, cs, -1, NULL, 0, NULL, NULL);//m_csFileName.GetLength()
WideCharToMultiByte(CP_ACP, 0, cs, -1, ch, len, NULL, NULL);
ch[len + 1] = '\0';
#endif
printf("%s\n", ch);
}
void chars_to_CString()
{
CString cs;
const char *ch = "this is test chars_to_CString";
const char ch2[] = "this is test chars_to_CString";
cs = ch;
//cs = ch2;
//cWnd->SetDlgItemText(IDC_EDIT_CSTRING_OUTPUT, cs);
}
void string_to_chars()
{
string ss = "this is test string_to_chars";
char ch[256];
memcpy(ch, ss.c_str(), ss.length() + 1);
printf("%s\n", ch);
}
void chars_to_string()
{
string ss;
const char *ch = "this is test chars_to_string";
const char ch2[] = "this is test chars_to_string";
ss = ch;
//ss = ch2;
cout << ss << endl;
}
void CString_to_string()
{
string ss;
CString cs = _T("this is test CString_to_string");
#if 1 //方法一
ss = CT2A(cs.GetString());
#else //方法二
#ifndef UNICODE
ss = CA2A(cs.GetString());
#else
ss = CW2A(cs.GetString());
#endif
#endif
cout << ss << endl;
}
void string_to_CString()
{
string ss = "this is test string_to_CString";
CString cs;
#if 1 //方法一
cs = CA2T(ss.c_str());
#else //方法二
#ifndef UNICODE
cs = CA2A(ss.c_str());
#else
cs = CA2W(ss.c_str());
#endif
#endif
//cWnd->SetDlgItemText(IDC_EDIT_CSTRING_OUTPUT, cs);
}
其他的还有char转int,string转int等等,相关资料包括demo代码请到我<MFC实用技巧>篇最下的网盘链接中下载!
1.String转int (1)Integer.parseInt() 或者 Integer.valueOf() (2)new Integer() 2.int 转String (1) (2) Integer.toString() (3) String.valueOf()...
1、QString 转 char * 2、char* 转 QString 3、QString 转 string 4、string 转 QString 5、string 转 char* 6、char* 转 st...
最近在做MFC的项目,遇到CString和char*互转的问题,项目中的字符集使用的是Unicode 字符集,遇到一些问题,记录一下。 MFC项目中的字符集可以设置两种:Unicode 字符集和多字节字符集。使用不同的字符集CString和char*互转的方式也不相同。 1、Unicode 字符集下CString和...
CString与 string 类型 互转 CString 转换为 string unicode 情况下面 //CA2T含义 //C:convert,转换的意思 //A:ANSI字符串,也就是Muti-Byte //2:to //T:中间类型,如果定义了_UNICODE,则T表示W;如果定义了_MBCS,则T表示A //W:宽字符串,也就是UNICODE...
类型转换目录 01. 三种类型介绍 1.1. char*类型 1.2. string类型 1.3. CString类型 02. 三种类型的互相转换 2.1 类型转换图 2.2 类型转换方法(six way) 2.2.1 string转换为CString 2.2.2 CString转换为string 2.2.3 char*转换为string 2.2.4 string转换为char* 2.2.5 ch...
环境:vs2010 1.CString转string 2.string转CString 3.CString转const char* 4. const char*转CString ...
cstring 转换为 cstring 转换为 * string 转换为 string 转换为 * LPCWSTR 转换为 LPCWSTR 转换为 * LPWSTR 转换为 LPWSTR 转换为 * LPCSTR 转换为 LPCSTR 转换为 * LPSTR 转换为 LPSTR 转换为 * char*转换为 char* 转换为 * /* char* 转换成LPCWSTR */ 参考:C++中cha...
1、char 转 CString 2、CString 转 char* 3、char 转 string 4、string 转 char* 5、string 转 CString 6、CString 转 string...