The following four functions are implemented separately:
Big Five yards turn gbk code/GBK five yards
GB2312 Code Transfer GBK Code/GBK code conversion GB2312 Code
//---------------------------------------------------------------------------
Big five yards to GBK code:
void __fastcall 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;
}
}
//---------------------------------------------------------------------------
GBK five yards big.
void __fastcall 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;
}
}
//----------------------------------------------------------------------------
GB2312 Code Transfer GBK code
void __fastcall 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;
}
}
//---------------------------------------------------------------------------
GBK Code Transfer GB2312 Code
void __fastcall 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;
}
}
//---------------------------------------------------------------------------
Test code
void __fastcall Tform1::button1click (tobject *sender)
{
Char szbuf[255];
Go from GB2312 to GBK
strcpy (Szbuf, Edit1->text.c_str ());
GB2GBK (SZBUF);
Edit2->text = String (SZBUF);
Transfer from GB2312 to BIG5, via GBK Relay
strcpy (Szbuf, Edit1->text.c_str ());
GB2GBK (SZBUF);
Gbk2big5 (SZBUF);
Edit3->text = String (SZBUF);
}