1. CString 转 char*
CString src = _T("CString to char");
int len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
char* dst = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, src, -1, dst, len, NULL, NULL );
delete[] dst;
char* CStrToChar(CString src)
{
USES_CONVERSION;
return T2A(src); // W2A(str);
}
2. CString 转 int
int CStrToInt(CString src)
{
return _ttoi(src);
}
3. 多字节和宽字节 互转
USES_CONVERSION;
LPWSTR w_str = A2W("Multibyte"); // 多字节转宽字节
LPCSTR c_str = W2A(L"Widebyte"); // 宽字节转多字节
wchar_t* AnsiToUnicode(char *str)
{
int cnt = MultiByteToWideChar (CP_ACP, 0, str, -1, NULL, 0);
wchar_t *wStr;
wStr = new wchar_t[cnt];
MultiByteToWideChar (CP_ACP, 0, str, -1, wStr, cnt);
return wStr;
}
int AnsiToUnicode(char *str, wchar_t* wStr, int size)
{
int cnt = MultiByteToWideChar (CP_ACP, 0, str, -1, NULL, 0);
if (cnt > size) return -1;
MultiByteToWideChar (CP_ACP, 0, str, -1, wStr, size);
return 0;
}
//Unicode转ansi
int UnicodeToAnsi(wchar_t *wStr, char* str, int size)
{
int cnt = WideCharToMultiByte(CP_OEMCP, NULL, wStr, -1, NULL, 0, NULL, FALSE);
if (cnt > size) return -1;
WideCharToMultiByte (CP_OEMCP, NULL, wStr, -1, str, size, NULL, FALSE);
return 0;
}
//Unicode转ansi
char* UnicodeToAnsi(wchar_t *wStr)
{
int Cnt = WideCharToMultiByte(CP_OEMCP, NULL, wStr, -1, NULL, 0, NULL, FALSE);
char *str;
str = new char[Cnt];
WideCharToMultiByte (CP_OEMCP, NULL, wStr, -1, str, Cnt, NULL, FALSE);
return str;
}