//---------------------------------------------------------------------------
//函数输入Big5字符,返回Gb简体字符
//---------------------------------------------------------------------------
AnsiString __fastcall Big2Gb(AnsiString sBig)
{
char* pszBig5=NULL; //Big5编码的字符
wchar_t* wszUnicode=NULL; //Unicode编码的字符
char* pszGbt=NULL; //Gb编码的繁体字符
char* pszGbs=NULL; //Gb编码的简体字符
AnsiString sGb; //返回的字符串
int iLen=0; //需要转换的字符数
pszBig5=sBig.c_str(); //读入需要转换的字符参数
//计算转换的字符数
iLen=MultiByteToWideChar (950, 0, pszBig5, -1, NULL,0) ;
//给wszUnicode分配内存
wszUnicode=new wchar_t[iLen+1];
//转换Big5码到Unicode码,使用了API函数MultiByteToWideChar
MultiByteToWideChar (950, 0, pszBig5, -1, wszUnicode,iLen);
//计算转换的字符数
iLen=WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, NULL,0, NULL, NULL) ;
//给pszGbt分配内存
pszGbt=new char[iLen+1];
//给pszGbs分配内存
pszGbs=new char[iLen+1];
//转换Unicode码到Gb码繁体,使用API函数WideCharToMultiByte
WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, pszGbt,iLen, NULL, NULL) ;
//转换Gb码繁体到Gb码简体,使用API函数LCMapString
LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE, pszGbt, -1, pszGbs, iLen);
//返回Gb码简体字符
sGb=pszGbs;
//释放内存
delete [] wszUnicode;
delete [] pszGbt;
delete [] pszGbs;
return sGb;
}
//---------------------------------------------------------------------------
//函数输入Gb字符,返回Big5字符
//---------------------------------------------------------------------------
AnsiString __fastcall Gb2Big(AnsiString sGb)
{
char* pszGbt=NULL; //Gb编码的繁体字符
char* pszGbs=NULL; //Gb编码的简体字符
wchar_t* wszUnicode=NULL; //Unicode编码的字符
char* pszBig5=NULL; //Big5编码的字符
AnsiString sBig5; //返回的字符串
int iLen=0; //需要转换的字符数
pszGbs=sGb.c_str(); //读入需要转换的字符参数
//计算转换的字符数
iLen=MultiByteToWideChar (936, 0, pszGbs, -1, NULL,0) ;
//给pszGbt分配内存
pszGbt=new char[iLen*2+1];
//转换Gb码简体到Gb码繁体,使用API函数LCMapString
LCMapString(0x0804,LCMAP_TRADITIONAL_CHINESE, pszGbs, -1, pszGbt, iLen*2);
//给wszUnicode分配内存
wszUnicode=new wchar_t[iLen+1];
//转换Gb码到Unicode码,使用了API函数MultiByteToWideChar
MultiByteToWideChar (936, 0, pszGbt, -1, wszUnicode,iLen);
//计算转换的字符数
iLen=WideCharToMultiByte (950, 0, (PWSTR) wszUnicode, -1, NULL,0, NULL, NULL) ;
//给pszBig5分配内存
pszBig5=new char[iLen+1];
//转换Unicode码到Big5码,使用API函数WideCharToMultiByte
WideCharToMultiByte (950, 0, (PWSTR) wszUnicode, -1, pszBig5,iLen, NULL, NULL) ;
//返回Big5码字符
sBig5=pszBig5;
//释放内存
delete [] wszUnicode;
delete [] pszGbt;
delete [] pszBig5;
return sBig5;
}
void BIG52GBK(char *szBuf)
{
if(!strcmp(szBuf, ""))
return;
int nStrLen = strlen(szBuf);
wchar_t *pws = new wchar_t[nStrLen + 1];
try
{
int nReturn = MultiByteToWideChar(950, 0, szBuf, nStrLen, pws, nStrLen + 1);
BOOL bValue = false;
nReturn = WideCharToMultiByte(936, 0, pws, nReturn, szBuf, nStrLen + 1, "?", &bValue);
szBuf[nReturn] = 0;
}
__finally
{
delete[] pws;
}
}
void GBK2BIG5(char *szBuf)
{
if(!strcmp(szBuf, ""))
return ;
int nStrLen = strlen(szBuf);
wchar_t *pws = new wchar_t[nStrLen + 1];
__try
{
MultiByteToWideChar(936, 0, szBuf, nStrLen, pws, nStrLen + 1);
BOOL bValue = false;
WideCharToMultiByte(950, 0, pws, nStrLen, szBuf, nStrLen + 1, "?", &bValue);
szBuf[nStrLen] = 0;
}
__finally
{
delete[] pws;
}
}
void GB2GBK(char *szBuf)
{
if(!strcmp(szBuf, ""))
return;
int nStrLen = strlen(szBuf);
WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
int nReturn = LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nStrLen, NULL, 0);
if(!nReturn)
return;
char *pcBuf = new char[nReturn + 1];
__try
{
wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
strncpy(szBuf, pcBuf, nReturn);
}
__finally
{
delete[] pcBuf;
}
}
void GBK2GB(char *szBuf)
{
if(!strcmp(szBuf, ""))
return;
int nStrLen = strlen(szBuf);
WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
int nReturn = LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nStrLen, NULL, 0);
if(!nReturn)
return;
char *pcBuf = new char[nReturn + 1];
__try
{
wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
strncpy(szBuf, pcBuf, nReturn);
}
__finally
{
delete []pcBuf;
}
}
// 调用示例....
char sourceEncode[255];
char szBuf[1024];
// 从 GB2312 转到 GBK
strcpy(szBuf, sourceEncode);
GB2GBK(szBuf);
// 从GB2312 转到 BIG5,通过 GBK 中转
strcpy(szBuf, sourceEncode);
GB2GBK(szBuf);
GBK2BIG5(szBuf);
......
}
转自:http://blog.csdn.net/kamplus/archive/2008/01/07/2029380.aspx