zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

C语言中文 实现 常见 三种
2023-09-27 14:27:55 时间
常见的中文内码一般有GB2312(简体中文),GBK和台湾那边用的BIG5(繁体中文),有时候看一些台湾编程论坛里的资料,都是乱码,如果在IE中浏览,则要求安装繁体字库的支持。网上也有很多中文内码的转换工具,但是在自己的程序中集成这些功能岂不是更好。

常见的中文内码一般有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()作为上述几个函数的传入参数。


c语言实现扫雷(含循环递归展开) 本笔记通过c语言实现扫雷小游戏(包含递归展开) 游戏实现逻辑位于test.c文件,整个游戏头文件位于game.h,游戏进程的具体操作于game.c中实现。
c语言实现三子棋(内含阅读思路,简单易实现) 本文如果按顺序来阅读可能不太好接受,建议阅读顺序为,由test.c的逻辑顺序读下去,遇见具体函数的实现跳转到game.c中来理解
c语言实现简单学生管理系统 该学生管理系统的实现是通过分文件的方式来写的,infor.h文件为头文件,源文件infor.c实现源文件test.c中封装函数,建议读者在做较大的系统是分文件来实现,可以提高代码的运行效率。