MultiByteToWideChar
WideCharToMultiByte
例程:
char ToGBTable[0xffff][3];
memset(ToGBTable, 0, sizeof(ToGBTable));
for(wchar i=0; i++; i<0xffff)
{
if (i<0xff)
{
ToGBTable[i][0] = (char)i;
}
else
{
WideCharToMultiByte(CP_ACP
WC_DEFAULTCHAR,
&i,
1,
ToGBTable[i],
3,
"?",
NULL);
}
}
CharToUnicode(BYTE* buf, DWORD bufSize)
{
CString csTemp;
WCHAR *UnicodeStr=new WCHAR[bufSize+1];
DWORD UnicodeSize=MultiByteToWideChar(CP_ACP,0,
(LPCSTR)buf,bufSize+1,UnicodeStr,(DWORD)bufSize+1);
csTemp=UnicodeStr;
delete [] UnicodeStr;
}
The Windows function MultiByteToWideChar converts multibyte-character strings to wide-character strings.
int MultiByteToWideChar(
UINT CodePage, // code page
DWORD dwFlags, // character-type options
LPCSTR lpMultiByteStr, // address of string to map
int cchMultiByte, // number of characters in string
LPWSTR lpWideCharStr, // address of wide-character buffer
int cchWideChar // size of buffer
);
#include <string.h>
#include "CharsetConversion.h"
int AsciiToUnicode(const char * szAscii, wchar_t * szUnicode)
{
int len, i;
if((szUnicode == NULL) || (szAscii == NULL))
return 0;
len = strlen(szAscii);
for(i=0;i<len+1;i++)
*szUnicode++ = static_cast<wchar_t>(*szAscii++);
return i;
}
int UnicodeToAscii(const wchar_t * szUnicode, char * szAscii)
{
int len, i;
if((szUnicode == NULL) || (szAscii == NULL))
return 0;
len = wcslen(szUnicode);
for(i=0;i<len+1;i++)
*szAscii++ = static_cast<char>(*szUnicode++);
return i;
}
_T or _TEXT
converts following character or string to its Unicode counterpart
HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{
ULONG cCharacters;
DWORD dwError;
if (NULL == pszA)
{
*ppszW = NULL;
return NOERROR;
}
cCharacters = strlen(pszA)+1;
*ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
if (NULL == *ppszW)
return E_OUTOFMEMORY;
if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,*ppszW, cCharacters))
{
dwError = GetLastError();
CoTaskMemFree(*ppszW);
*ppszW = NULL;
return HRESULT_FROM_WIN32(dwError);
}
return NOERROR;
}