在工作中经常会用到GB2312和BIG5,Unicode/UTF8等编码之间的互相转化,也有很多程序员对这个问题不断的提问,现在我把我的
实现告诉大家.
#include "Utf8.h"
void ConvertGBKToUtf8(char *strGBK , int maxlen )
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
if (strlen( szUtf8) >= (unsigned)maxlen)
{
strncpy( strGBK , szUtf8 , maxlen -1 );
strGBK[ maxlen -2 ] = 0 ;
strGBK[ maxlen -1 ] = 0 ;
}
strcpy( strGBK , szUtf8 );
// if (wcslen( szUtf8 ) > maxlen )
// {
//
// }
// wcscpy(strGBK, szUtf8);
delete[] szUtf8;
delete[] wszUtf8;
}
void ConvertUtf8ToGBK(char *strUtf8 , int maxlen )
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
if ( strlen( szGBK ) >= (unsigned)maxlen )
{
strncpy( strUtf8 , szGBK , maxlen - 1 ) ;
strUtf8[maxlen - 1 ] = 0 ;
}
else
{
strcpy(strUtf8, szGBK);
}
delete[] szGBK;
delete[] wszGBK;
}
wchar_t* GB2312ToUnicode(const char* szGBString)
{
UINT nCodePage = 936; //GB2312
int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0);
wchar_t* pBuffer = new wchar_t[nLength+1];
MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength);
pBuffer[nLength]=0;
return pBuffer;
}
wchar_t* BIG5ToUnicode(const char* szBIG5String)
{
UINT nCodePage = 950; //BIG5
int nLength=MultiByteToWideChar(nCodePage,0,szBIG5String,-1,NULL,0);
wchar_t* pBuffer = new wchar_t[nLength+1];
MultiByteToWideChar(nCodePage,0,szBIG5String,-1,pBuffer,nLength);
pBuffer[nLength]=0;
return pBuffer;
}
char* UnicodeToGB2312(const wchar_t* szUnicodeString)
{
UINT nCodePage = 936; //GB2312
int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
char* pBuffer=new char[nLength+1];
WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
pBuffer[nLength]=0;
return pBuffer;
}
char* UnicodeToBIG5(const wchar_t* szUnicodeString)
{
UINT nCodePage = 950; //BIG5
int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
char* pBuffer=new char[nLength+1];
WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
pBuffer[nLength]=0;
return pBuffer;
}
char* BIG5ToGB2312(const char* szBIG5String)
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);
wchar_t* szUnicodeBuff = BIG5ToUnicode(szBIG5String);
char* szGB2312Buff = UnicodeToGB2312(szUnicodeBuff);
int nLength = LCMapString(lcid,LCMAP_SIMPLIFIED_CHINESE, szGB2312Buff,-1,NULL,0);
char* pBuffer = new char[nLength + 1];
LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE,szGB2312Buff,-1,pBuffer,nLength);
pBuffer[nLength] = 0;
delete[] szUnicodeBuff;
delete[] szGB2312Buff;
return pBuffer;
}
char* GB2312ToBIG5(const char* szGBString)
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);
int nLength = LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,NULL,0);
char* pBuffer=new char[nLength+1];
LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,pBuffer,nLength);
pBuffer[nLength]=0;
wchar_t* pUnicodeBuff = GB2312ToUnicode(pBuffer);
char* pBIG5Buff = UnicodeToBIG5(pUnicodeBuff);
delete[] pBuffer;
delete[] pUnicodeBuff;
return pBIG5Buff;
}