下面是UNICODE到ANSI轉換的函數:
void WCharToAChar(wchar_t* wchar,char* str,int size)//UNICODE to ANSI
{
memset(str,0,size);
DWORD flag = WideCharToMultiByte(CP _ OEMCP,NULL,wchar,-1,NULL,0,NULL,FALSE);
WideCharToMultiByte(CP_OEMCP,NULL,wchar,-1,str,flag,NULL,FALSE);
}
void ACharToWChar(char* str,wchar_t* wchar,int size) //ANSI到UNICODE
{
_wcsnset(wchar,0,size);
DWORD flag = MultiByteToWideChar(CP _ ACP,0,str,-1,NULL,0);
MultiByteToWideChar (CP_ACP,0,str,-1,wchar,size);
}
UTF-8到UNICODE:
PContent是壹個char數組,包含UTF-8字符內容,Len是它的長度。
int n = MultiByteToWideChar(CP _ utf8,0,pContent,Len,NULL,0);
wchar _ t * pWideChar =(wchar _ t *)calloc(n+1,sizeof(wchar _ t));
MultiByteToWideChar(CP_UTF8,0,pContent,Len,pWideChar,n);
然後用上面的WCharToAChar把pContent轉換成Char。