(1)
int main(void)
{
char str [ 256 ] = "唐志国" ; //一段UTF-8编码
WCHAR* strA;
int i= MultiByteToWideChar ( CP_ACP , 0 ,(char*) str ,-1 ,NULL,0);
strA = new WCHAR[i];
MultiByteToWideChar ( CP_ACP , 0 ,( char * ) str, -1, strA , i);
i= WideCharToMultiByte(CP_UTF8,0,strA,-1,NULL,0,NULL,NULL);
char *strB=new char[i];
WideCharToMultiByte (CP_UTF8,0,strA,-1,strB,i,NULL,NULL);
//strB即为所求
delete []strA;
delete []strB;
return 0;
}
(2)
int main(void)
{
char str [ 256 ] = "唐志国" ;
WCHAR* strA;
int i= MultiByteToWideChar ( CP_ACP , 0 ,(char*) str ,-1 ,NULL,0);
strA = new WCHAR[i];
MultiByteToWideChar ( CP_ACP , 0 ,( char * ) str, -1, strA , i);
i= WideCharToMultiByte(CP_UTF8,0,strA,-1,NULL,0,NULL,NULL);
char *strB=new char[i];
WideCharToMultiByte (CP_UTF8,0,strA,-1,strB,i,NULL,NULL);
//strB即为所求
delete []strA;
delete []strB;
return 0;
}
(3)
int main(void)
{
char str [ 256 ] = "唐志国" ;
WCHAR* strA;
int i= MultiByteToWideChar ( CP_ACP , 0 ,(char*) str ,-1 ,NULL,0);
strA = new WCHAR[i];
MultiByteToWideChar ( CP_ACP , 0 ,( char * ) str, -1, strA , i);
i= WideCharToMultiByte(CP_UTF8,0,strA,-1,NULL,0,NULL,NULL);
char *strB=new char[i];
WideCharToMultiByte (CP_UTF8,0,strA,-1,strB,i,NULL,NULL);
//strB即为所求
delete []strA;
delete []strB;
return 0;
}
(4)
这里面包含了两个方向的转换,你看一下吧
#include "Windows.h"
int main(void)
{
char str [ 256 ] = "唐志国" ; //一段UTF-8编码
WCHAR* strA;
int i= MultiByteToWideChar ( CP_ACP , 0 ,(char*) str ,-1 ,NULL,0);
strA = new WCHAR[i];
MultiByteToWideChar ( CP_ACP , 0 ,( char * ) str, -1, strA , i);
i= WideCharToMultiByte(CP_UTF8,0,strA,-1,NULL,0,NULL,NULL);
char *strB=new char[i];
WideCharToMultiByte (CP_UTF8,0,strA,-1,strB,i,NULL,NULL);
//strB即为所求
delete []strA;
i= MultiByteToWideChar ( CP_UTF8 , 0 ,(char*) strB ,-1 ,NULL,0);
strA = new WCHAR[i];
MultiByteToWideChar ( CP_UTF8 , 0 ,( char * ) strB, -1, strA , i);
delete []strB;
i= WideCharToMultiByte(CP_ACP,0,strA,-1,NULL,0,NULL,NULL);
strB=new char[i];
WideCharToMultiByte (CP_ACP,0,strA,-1,strB,i,NULL,NULL);
//strB即为所求
delete []strA;
delete []strB;
return 0;
}
(5)
这是转换函数:
int CodePageConvert(UINT SrcCodePage, LPCTSTR pBuff, int iBuffLen, UINT DestCodePage, char* &lpCodePage)
{
int iWideCharCnt = ::MultiByteToWideChar(SrcCodePage, 0, pBuff, iBuffLen, NULL, 0);
LPWSTR lpszWideChar = new wchar_t[iWideCharCnt + 1];
memset(lpszWideChar, 0, (iWideCharCnt + 1) * sizeof(WCHAR));
iWideCharCnt = MultiByteToWideChar(SrcCodePage, 0, pBuff, iBuffLen, lpszWideChar, iWideCharCnt);
if(DestCodePage == 54936
&& !IsValidCodePage(54936))
DestCodePage = 936;
int iDestCnt = WideCharToMultiByte(DestCodePage, 0, lpszWideChar, iWideCharCnt, NULL, 0, NULL, NULL);
lpCodePage = new char[iDestCnt + 1];
memset(lpCodePage, 0, iDestCnt + 1);
iDestCnt = WideCharToMultiByte(DestCodePage, 0, lpszWideChar, iWideCharCnt, lpCodePage, iDestCnt, NULL, NULL);
delete []lpszWideChar;
return iDestCnt;
}
下面是调用方法:
utf-8 到 gbk
int nLen = CodePageConvertUnix("UTF-8",_T("标准"),2,"GBK",lpOut);
gbk 到utf-8
int nLen = CodePageConvertUnix("UTF-8",_T("标准"),2,"GBK",lpOut);