不管是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
GBK->UNICODE : MultiByteToWideChar
- 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);
- }