UTF8 To GBK 转换程序

时间:2022-12-16 14:08:08

最近由于业务的需要,自己手写了一个UTF8到GBK相互转换的程序,分别为linux版本和windows版本,需要注意的是:

不管是UTF8到GBK还是GBK到UTF8,中间都必须转换到UNICODE,然后再从UNICODE转换到目标代码:

从UTF8到GBK的转换有固定的算法可以转换过去,但是从GBK到UNICODE和从UNICODE到GBK必须使用系统所提供的函数:

其中标准c提供了相应的函数分别是:

GBK—>UNICODE :mbstowcs

UNICODE->GBK : wcstombs

需要注意的是在使用上面两个函数之前必须使用setlocale函数制定所使用的字符集为GBK

另外在windows系统上,还提供了两个操作系统级别的函数,分别是:

UNICODE->GBK  :WideCharToMultiByte

UNICODE->GBK : MultiByteToWideChar

以下为windows下的源码:

 
 
#include "windows.h"

#include <string.h>
#include <stdlib.h>
#include <locale.h>
char* GB18030ToUTF_8(const char *pText,int pLen);
char* UTF_8ToGB18030(const char *pText,int pLen);
void GB18030ToUnicode(wchar_t* pOut,const char *gbBuffer);
void UnicodeToUTF_8(char* pOut,WCHAR* pText);
void UnicodeToGB18030(char* pOut,unsigned short* uData) ;
void UTF_8ToUnicode(wchar_t* pOut,const char *pText);


char* GB18030ToUTF_8(const char *pText,int pLen)
{
    char buf[1024];
    char* rst = new char[pLen+(pLen>>2)+2];
    
    memset(buf,0,1024);
    
    memset(rst,0,pLen+(pLen>>2)+2);
        
    int i = 0;
    
    int j = 0;
    
    while(i < pLen)    
    {
        if( *(pText + i) >= 0)        
        {    
            rst[j++] = pText[i++];    
        }    
        else        
        {    
            wchar_t pbuffer;
            GB18030ToUnicode(&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;        
        }    
    }
    
    strcpy(&rst[j],"/0"); 
    return rst;
} 

void GB18030ToUnicode(wchar_t* pOut,const char *gbBuffer) 
{
    ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
} 

void UnicodeToUTF_8(char* pOut,wchar_t* pText) 
{  
        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));
} 


char* UTF_8ToGB18030(const char *pText,int pLen) 

{    
    char * newBuf = new char[pLen];

    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_t Wtemp;
            
            UTF_8ToUnicode(&Wtemp,pText+i);
            
            UnicodeToGB18030(Ctemp,&Wtemp);
            
            newBuf[j] = Ctemp[0];
            
            newBuf[j+1] = Ctemp[1];
            
            i+= 3;
            
            j+= 2;
            
        }
        
    }
    strcpy(&newBuf[j],"/0");
    
    return newBuf;

} 

void UTF_8ToUnicode(wchar_t* pOut,const 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);
}


void UnicodeToGB18030(char* pOut,unsigned short* uData) 
{
    ::WideCharToMultiByte(CP_ACP,NULL,LPCWSTR(uData),1,pOut,sizeof(WCHAR),NULL,NULL);
}   



 

以下是linux系统上的源码:

void UnicodeToUTF_8(char* pUTF8,wchar_t* pUnicode)
{
    char* pchar = (char*)pUnicode;
        pUTF8[0] = (0xE0|((pchar[1]&0xF0)>>4));

        pUTF8[1] = (0x80|((pchar[1]&0x0F)<<2))+((pchar[0]&0xC0)>>6);

        pUTF8[2] = (0x80|(pchar[0]&0x3F));
}

char* GBToUTF_8(const char* pText, int pLen)
{
    setlocale(LC_ALL,"zh_CN.gbk");
    char buf[1024];
    char* rst = new char[pLen + (pLen >> 2) + 2];
    memset(rst,0,pLen + (pLen >> 2) +2);
    int i = 0; int j = 0;
    while(i < pLen)
    {
        if(*(pText + i) >=0 )
        {
            rst[j++] = pText[i++];
        }
        else
        {
            wchar_t pwbuffer;
            mbstowcs(&pwbuffer,pText + i,1);
            UnicodeToUTF_8(buf,&pwbuffer);
            unsigned short tmp = 0;
            rst[j] = buf[0];
            rst[j+1] = buf[1];
            rst[j+2] = buf[2];
            j+=3;
            i+=2;        
        }
    }
    return rst;
}

char* UTF_8ToGB(const char* pText,int pLen)
{
    char* newBuf = new char[pLen];
    char temp[4];
    
    memset(newBuf,0,pLen);
    memset(temp,0,4);

    int i = 0;
    int j = 0;

    setlocale(LC_ALL,"zh_CN.gbk");

    while(i < pLen)
    {
        if(pText[i] > 0)
        {
            newBuf[j++] = pText[i++];
        }
        else
        {
            wchar_t wTemp;
            UTF_8ToUnicode(&wTemp,pText+i);
            wcstombs(temp,&wTemp,2);
            newBuf[j] = temp[0];
            newBuf[j+1] = temp[1];
            i+= 3;
            j+= 2;
        }
    }
    return newBuf;
}

void UTF_8ToUnicode(wchar_t* pUnicode,const char* pUTF_8)
{
    char* uchar = (char*)pUnicode;
    uchar[1] =  ((pUTF_8[0]&0x0F)<<4)+((pUTF_8[1]>>2)&0x0F);
    uchar[0] =  ((pUTF_8[1]&0x03)<<6)+(pUTF_8[2]&0x3F);
}