16,473
社区成员
发帖
与我相关
我的任务
分享
wstring ansi_2_unicode(const char *p_in)
{
int nLen = MultiByteToWideChar(CP_ACP, 0, p_in, -1, NULL, 0);
wchar_t *p_out = new wchar_t[nLen];
memset(p_out, 0, nLen);
MultiByteToWideChar(CP_ACP, 0, p_in, -1, p_out, nLen);
wstring s_out(p_out);
SAFE_DELETE_ARRAY(p_out);
return s_out;
}
char *pszText = "ANSI字符串转换为UNICODE字符串!";
int cchWideChar = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
PTCHAR ptszText = NULL; // 这里你应该写PWCHAR,或者wchar_t* ptszText = NULL;
ptszText = new TCHAR[cchWideChar];
// 这里下面没必要,如果ptszText为NULL,下面的delete完全没有必要,delete空指针不会出错
if(!ptszText){
delete [] ptszText;
}
MultiByteToWideChar(CP_ACP, 0, pszText, -1, ptszText, cchWideChar);
MessageBox(ptszText);
delete [] ptszText;
#include <atlbase.h>
//使用ATL的W2A和A2W宏必须使用USES_CONVERSION
USES_CONVERSION;
//Unicode字符串
wchar_t* wszText=L"1.Unicode字符转换为ANSI;";
printf("%s\n",W2A(wszText));
//用wprintf输出非英文字符,需要设置当前的地域信息
setlocale(LC_ALL,"chs");
//ANSI字符串(ANSI:American National Standards Institute)
//中文内码MBCS:Multi-Byte character sets,英文内码SBCS:Single-Byte character sets)
char* szText="2.ANSI字符转换成Unicode.";
wprintf(L"%s\n",A2W(szText));
//将ANSI字符串,转成UNICODE
#define AnsiStr2UnicodeStr(szA, szW) { int nLenOfWideCharStr; nLenOfWideCharStr = MultiByteToWideChar(CP_ACP, 0, szA, -1, NULL, 0); MultiByteToWideChar(CP_ACP, 0, szA, -1, szW, nLenOfWideCharStr);}
//将UNICODE字符串,转成ANSI
#define UnicodeStr2AnsiStr(pA, pW, pAStrLen){ int ret = WideCharToMultiByte(CP_ACP, 0, pW, -1, pA, 1024, NULL, NULL); *pAStrLen = ret-1; }
//将CString转换成char*
#define CStr2AnsiStr(cstr, pszA, pAStrLen){ LPCWSTR lpwName = cstr.GetBuffer(); UnicodeStr2AnsiStr(pszA, lpwName, &pAStrLen);}
//将char*转换成CString
#define AnsiStr2CStr(cstr, pszA){ WCHAR wstrName[1024] = {0};AnsiStr2UnicodeStr(pszA, wstrName);cstr.Format(L"%s", wstrName);}
char *pszText = "ANSI×Ö·û´®×ª»»ÎªUNICODE×Ö·û´®£¡";
int cchWideChar = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
wchar_t* ptszText = NULL;
ptszText = new wchar_t[cchWideChar];
// 或者 ptszText = (wchar_t*)malloc(cchWideChar * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, pszText, -1, ptszText, cchWideChar);
AfxMessageBox(CString(ptszText));
delete [] ptszText;
ptszText = NULL;