
时不常就要用一下,老是记不住,放空间里,做个备忘录了。
1。char数组转化为CString。
(1)方法一:
char cBuf[] = {0x31,0x32,0x33}; // _T("123")
CString str = _T("");
#ifdef _UNICODE
// Ansi码转化为unicode码
WCHAR wTmp[BUF_SIZE] = {};
// 转换为unicode码
if (0 == MultiByteToWideChar(CP_ACP,0,(LPCSTR)cBuf,-1,wTmp,BUF_SIZE)) {
assert(false);
} else {
CString strTmp(wTmp);
}
#else
CString strTmp(cBuf);
#endif // _UNICODE
(2)方法二:
char cBuf[] = {0x31,0x32,0x33}; // _T("123")
CString str = _T("");
#ifdef _UNICODE
USES_CONVERSION;
LPTSTR pStr = A2W((char*)cBuf);
str.Format(_T("%s"),pStr);
#else
str.Format(_T("%s"),cBuf);
#endif // _UNICODE
2。ansi和unicode互转工具--免杀版rarwchar数组转化为CString。
(1)方法一:
wchar cBuf[] = {0x31,0x00,0x32,0x00,0x33,0x00}; // _T("123")
CString str = _T("");
#ifdef _UNICODE
CString strTmp(cBuf);
#else

// unicode码转化为Ansi码
char aTmp[BUF_SIZE] = {};
// 转换为ANSI码
if (0 == WideCharToMultiByte(CP_ACP,0,cBuf,-1,aTmp,BUF_SIZE,NULL,NULL)) {
assert(false);
} else {
CString strTmp(aTmp);
}
#endif // _UNICODE
(2)方法二:
wchar cBuf[] = {0x31,0x00,0x32,0x00,0x33,0x00}; // _T("123")
CString str = _T("");
#ifdef _UNICODE
CString strTmp(cBuf);
#else
// unicode码转化为Ansi码
USES_CONVERSION;
char* pStr = W2A(strAlarmId);
str.Format(_T("%s"),pStr);
#endif // _UNICODE
//-----------------------------------------------------总结-------------------------------------------------------------
ansi以2个字节表示一个字符,unicode以一个字节表示一个字符。比如'0',unicode编码表示为0x0030,而ansi表示为0x30,就这个差别。但是表现在文件中时,一般区分文件是unicode还是ansi,就是要看文件的开始两个字节是不是0xFEFF,是则为unicode否即为ansi。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-44502-1.html
更环保
兴风作浪