字符集编码之间的转换,UTF-8 转为 GB2312,GB2312 转为 UTF-8 ,Unicode 转换成UTF-8

来源:互联网 发布:淘宝秒杀验证码怎么输 编辑:程序博客网 时间:2024/04/27 20:17
/*
李大叔2015-5-5修正
实现常见字符集编码直接的转换,主要的工作是在Unicode和UTF-8之间的转换,原理如下,
代码是引用网上一个人写的,但是里面有个内存错误的bug,修正后如下。


原理:
在0800-FFFF之间,所以要用3字节模板:1110xxxx 10xxxxxx 10xxxxxx。
某字unicode二进制是:0110 1100 0100 1001,将这个比特流按三字节模板的分段方法分为0110 110001 001001,
依次代替模板中的x,得到:1110-0110 10-110001 10-001001,即E6 B1 89,这就是其UTF8的编码。
*/
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
 /////////UTF8TOGB.h////////////////////////// 
 void UTF_8ToUnicode(WCHAR* pOut,char *pText)
 {
  char* uchar = (char *)pOut;
 
  uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
  uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);


  return;
 }
// Unicode 转换成UTF-8 
void UnicodeToUTF_8(char* pOut,WCHAR* pText)
{
  // 注意 WCHAR高低字的顺序,低字节在前,高字节在后
  char* pchar = (char *)pText;


  pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
  pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
  pOut[2] = (0x80 | (pchar[0] & 0x3F));


  return;
}
  
// 把Unicode 转换成 GB2312 
void UnicodeToGB2312(char* pOut,unsigned short uData)
{
 ::WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
 return;
}     


// GB2312 转换成 Unicode
void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer)
{
 ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
 return;
}


//GB2312 转为 UTF-8
void GB2312ToUTF_8(std::string& pOut,char *pText, int pLen)
{
  char buf[4];
  char* rst = new char[pLen + (pLen >> 2) + 3];
  
  memset(buf,0,4);
  memset(rst,0,pLen + (pLen >> 2) + 3);
  
  int i = 0;
  int j = 0;
  while(i < pLen)
  {
          //如果是英文直接复制就可以
            if( *(pText + i) >= 0)
            {
                    rst[j++] = pText[i++];
            }
            else
            {
                    WCHAR pbuffer;
                    Gb2312ToUnicode(&pbuffer,pText+i);
                    
                    UnicodeToUTF_8(buf,&pbuffer);
                    
                    unsigned short int tmp = 0;
                    tmp = rst[j] = buf[0];
                    tmp = rst[j+1] = buf[1];
                    tmp = rst[j+2] = buf[2];
                    
                    
                    j += 3;
                    i += 2;
            }
    }
    rst[j] = '\0';
    //返回结果
    pOut = rst;             
    delete rst;   
    
    return;
  }
  
  //UTF-8 转为 GB2312
 void UTF_8ToGB2312(string& pOut, char *pText, int pLen)
  {
      char * newBuf = new char[pLen+1];
      char Ctemp[4];
      memset(Ctemp,0,4);
 
      int i =0;
      int j = 0;
      
      while(i < pLen)
      {
             if(pText[i] > 0)
             {
                     newBuf[j++] = pText[i++];                       
             }
             else                 
             {
                     WCHAR Wtemp;
                     UTF_8ToUnicode(&Wtemp,pText + i);
             
                     UnicodeToGB2312(Ctemp,Wtemp);
                 
                     newBuf[j] = Ctemp[0];
                     newBuf[j + 1] = Ctemp[1];
 
                     i += 3;    
                     j += 2;   
             }
      }
      newBuf[j] = '\0';
      pOut="";
 pOut.assign(newBuf);
     
      delete []newBuf;
      
      return; 
 }


void main(int argc,char* argv[])
{
    ifstream ifile;
ofstream ofile;


ifile.open("testword.txt",ios::in);/*打开需要转换的文本,内容是utf8编码*/
ofile.open("result.txt",ios::out|ios::trunc);/*新建存储ASNI(gb2312)的文本*/


if(ifile.fail()|| ofile.fail())//如果打开失败就退出
{
return;
}


string in;
string out;
char *pText;


getline(ifile,in);


in.erase(0,3);/*utf8文本的前三个字节是utf8编码的标识*/
do  
{
pText=(char *)in.c_str();
UTF_8ToGB2312(out,pText, strlen(in.c_str()));/* 将pText指向的内容转换为asni存储在out里面 */

ofile.write(out.c_str(),strlen(out.c_str()));
ofile.write("\r\n",2);
out="";
}while( getline(ifile,in) );/* 按行读取内容 */


ifile.close();
ofile.close();
   
}
0 0
原创粉丝点击