C language realizes the transformation between five big codes of gbk/gb2312/

Source: Internet
Author: User
Tags bool strcmp strlen

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.