• 三种常见中文内码的转换方法


    我们平时常见的三种中文内码是:GB2312(简体中文)、GBK、BIG5(繁体中文)。网上有很多中文内码的专用转换工具。我们碰到由于内码不 一致而导致的乱麻问题,用这些工具可以进行相互转换。但论坛里经常有人问如何在自己的程序中集成这些功能呢?本文将介绍如何利用 Windows 提供的API 函数来实现。转换涉及到的 API 函数主要有两个:MultiByteToWideChar 和 WideCharToMultiByte。有关这两个函数的详细文档请参考 MSDN,本文不再赘述。

      本文将介绍四个转换函数分别实现如下的转换:

        * Big5 => GBK
        * GBK => Big5
        * GB2312 => GBK
        * GBK => GB2312

      有关 GB2312 =〉BIG5 的转换以及 BIG5 =〉GB2312 的转换可以通过 GBK 间接实现。先将 GB2312 转成 GBK,再将 GBK 转成 BIG5,反之亦然。当然也可以自己实现它们之间的直接转换。

    // Big5 => GBK:   
      
    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;   
      }   
    }   
      
    //---------------------------------------------------------------------------   
    // GBK => Big5   
      
    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;   
      }   
    }   
      
    //----------------------------------------------------------------------------   
    // GB2312 => GBK   
    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;   
      }   
    }   
      
    //---------------------------------------------------------------------------   
    // GBK =〉GB2312   
      
    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);   
         
       ......   
      
    }  
    
    Life is like a box of chocolate, you never know what you are going to get.
  • 相关阅读:
    MFC深入浅出读书笔记第一部分
    给对话框添加背景
    相对路径与绝对路径之比较
    线程同步的四种方式以及串口通信
    DirectShow简单入门程序
    两个对话框之间的通信
    websphere部署war包
    Struts2的基础知识
    iBatis基础知识
    Struts1的基础知识
  • 原文地址:https://www.cnblogs.com/mars9/p/2506294.html
Copyright © 2020-2023  润新知