用C语言实现常见的三种中文内码转换

常见的中文内码一般有GB2312(简体中文),GBK和台湾那边用的BIG5(繁体中文),有时候看一些台湾编程论坛里的资料,都是乱码,如果在IE中浏览,则要求安装繁体字库的支持。网上也有很多中文内码的转换工具,但是在自己的程序中集成这些功能岂不是更好。Windows中提供了MultiByteToWideChar和WideCharToMultiByte两兄弟函数,足可以搞定这些功能了。

以下四个函数分别实现:
大五码转GBK码/GBK转大五码
GB2312码转GBK码/GBK码转GB2312码

于是有人要问了,为什么没有GB2312转BIG5和BIG5转GB2312呢,我们有GBK,可以做一下中转啊。可以将GB2312转成GBK,再将GBK转成BIG5,反之亦然。如果你嫌麻烦,可以自己写一个GB2BIG5/BIG52GB。


//---------------------------------------------------------------------------

 

None.gif  //   大五码转GBK码:
None.gif
None.gif
  //   い地チ㎝瓣 --> 中華人民共和國 
None.gif 

None.gif  void  __fastcall BIG52GBK(  char   * szBuf)
None.gif
ExpandedBlockStart.gif  {
InBlock.gif
InBlock.gif  if ( ! strcmp(szBuf,  "" ))
InBlock.gif
InBlock.gif return ;
InBlock.gif
InBlock.gif  int  nStrLen  =  strlen(szBuf);
InBlock.gif
InBlock.gif wchar_t  * pws  =   new  wchar_t[nStrLen  +   1 ];
InBlock.gif
InBlock.gif  try 
InBlock.gif 
ExpandedSubBlockStart.gif   {
InBlock.gif
InBlock.gif int  nReturn  =  MultiByteToWideChar( 950 ,  0 , szBuf, nStrLen, pws, nStrLen  +   1 );
InBlock.gif
InBlock.gifBOOL bValue  =   false ;
InBlock.gif
InBlock.gifnReturn  =  WideCharToMultiByte( 936 ,  0 , pws, nReturn, szBuf, nStrLen  +   1 ,  " ? " ,  & bValue);
InBlock.gif
InBlock.gifszBuf[nReturn]  =   0 ;
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
InBlock.gif __finally
InBlock.gif
ExpandedSubBlockStart.gif  {
InBlock.gif
InBlock.gifdelete[] pws;
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
ExpandedBlockEnd.gif
None.gif

 

//---------------------------------------------------------------------------

 

None.gif  //   GBK转大五码
None.gif
None.gif
  //   中華人民共和國 --> い地チ㎝瓣 
None.gif 

None.gif  void  __fastcall GBK2BIG5(  char   * szBuf)
None.gif
ExpandedBlockStart.gif  {
InBlock.gif
InBlock.gif  if ( ! strcmp(szBuf,  "" ))
InBlock.gif
InBlock.gif return  ;
InBlock.gif
InBlock.gif  int  nStrLen  =  strlen(szBuf);
InBlock.gif
InBlock.gif wchar_t  * pws  =   new  wchar_t[nStrLen  +   1 ];
InBlock.gif
InBlock.gif  try 
InBlock.gif 
ExpandedSubBlockStart.gif   {
InBlock.gif
InBlock.gifMultiByteToWideChar( 936 ,  0 , szBuf, nStrLen, pws, nStrLen  +   1 );
InBlock.gif
InBlock.gifBOOL bValue  =   false ;
InBlock.gif
InBlock.gifWideCharToMultiByte( 950 ,  0 , pws, nStrLen, szBuf, nStrLen  +   1 ,  " ? " ,  & bValue);
InBlock.gif
InBlock.gifszBuf[nStrLen]  =   0 ;
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
InBlock.gif __finally
InBlock.gif
ExpandedSubBlockStart.gif  {
InBlock.gif
InBlock.gifdelete[] pws;
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
ExpandedBlockEnd.gif
None.gif

 

//----------------------------------------------------------------------------

 

None.gif  //   GB2312码转GBK码
None.gif
None.gif
  //   中华人民共和国 --> 中華人民共和國 
None.gif 

None.gif  void  __fastcall GB2GBK(  char   * szBuf)
None.gif
ExpandedBlockStart.gif  {
InBlock.gif
InBlock.gif  if ( ! strcmp(szBuf,  "" ))
InBlock.gif
InBlock.gif return ;
InBlock.gif
InBlock.gif  int  nStrLen  =  strlen(szBuf);
InBlock.gif
InBlock.gif WORD wLCID  =  MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
InBlock.gif
InBlock.gif  int  nReturn  =  LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nStrLen, NULL,  0 );
InBlock.gif
InBlock.gif  if ( ! nReturn)
InBlock.gif
InBlock.gif return ;
InBlock.gif
InBlock.gif  char   * pcBuf  =   new   char [nReturn  +   1 ];
InBlock.gif
InBlock.gif  try 
InBlock.gif 
ExpandedSubBlockStart.gif   {
InBlock.gif
InBlock.gifwLCID  =  MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
InBlock.gif
InBlock.gifLCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nReturn, pcBuf, nReturn  +   1 );
InBlock.gif
InBlock.gifstrncpy(szBuf, pcBuf, nReturn);
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
InBlock.gif __finally
InBlock.gif
ExpandedSubBlockStart.gif  {
InBlock.gif
InBlock.gifdelete[] pcBuf;
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
ExpandedBlockEnd.gif
None.gif

 

//---------------------------------------------------------------------------

 

None.gif  //   GBK码转GB2312码
None.gif
None.gif
  //   中華人民共和國 --> 中华人民共和国 
None.gif 

None.gif  void  __fastcall GBK2GB(  char   * szBuf)
None.gif
ExpandedBlockStart.gif  {
InBlock.gif
InBlock.gif  if ( ! strcmp(szBuf,  "" ))
InBlock.gif
InBlock.gif return ;
InBlock.gif
InBlock.gif  int  nStrLen  =  strlen(szBuf);
InBlock.gif
InBlock.gif WORD wLCID  =  MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
InBlock.gif
InBlock.gif  int  nReturn  =  LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nStrLen, NULL,  0 );
InBlock.gif
InBlock.gif  if ( ! nReturn)
InBlock.gif
InBlock.gif return ;
InBlock.gif
InBlock.gif  char   * pcBuf  =   new   char [nReturn  +   1 ];
InBlock.gif
InBlock.gif  try 
InBlock.gif 
ExpandedSubBlockStart.gif   {
InBlock.gif
InBlock.gifwLCID  =  MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
InBlock.gif
InBlock.gifLCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nReturn, pcBuf, nReturn  +   1 );
InBlock.gif
InBlock.gifstrncpy(szBuf, pcBuf, nReturn);
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
InBlock.gif __finally
InBlock.gif
ExpandedSubBlockStart.gif  {
InBlock.gif
InBlock.gifdelete []pcBuf;
InBlock.gif
ExpandedSubBlockEnd.gif } 
InBlock.gif 
ExpandedBlockEnd.gif
None.gif

 

//---------------------------------------------------------------------------

 

None.gif  //   测试代码 
None.gif 

None.gif  void  __fastcall TForm1::Button1Click(TObject  * Sender)
None.gif
ExpandedBlockStart.gif  {
InBlock.gif
InBlock.gif  char  szBuf[ 255 ];
InBlock.gif
InBlock.gif  //  从GB2312转到GBK 
InBlock.gif 

InBlock.gif strcpy(szBuf, Edit1 -> Text.c_str());
InBlock.gif
InBlock.gif GB2GBK(szBuf);
InBlock.gif
InBlock.gif Edit2 -> Text  =  String(szBuf);
InBlock.gif
InBlock.gif  //  从GB2312转到BIG5,通过GBK中转 
InBlock.gif 

InBlock.gif strcpy(szBuf, Edit1 -> Text.c_str());
InBlock.gif
InBlock.gif GB2GBK(szBuf);
InBlock.gif
InBlock.gif GBK2BIG5(szBuf);
InBlock.gif
InBlock.gif Edit3 -> Text  =  String(szBuf);
InBlock.gif
ExpandedBlockEnd.gif
None.gif


 
注意,请不要使用String类的c_str()作为上述几个函数的传入参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值