在做关于字符识别和应用的过程中常常遇到UFT8和GB232之间的转换,至于什么是UTF8,什么是GB232这个我就不细细的说了,后续有时间在补上,在这里主要是贴上自己的转换代码,后续在使用的时候自己能清楚的知道,对其它的在使用这一块的朋友也有个帮助~!
void UTF_8ToUnicode(wchar_t* 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); } void UnicodeToUTF_8(char* pOut,wchar_t* 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)); } void UnicodeToGB2312(char* pOut,wchar_t uData) { WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL); } void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer) { ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1); } void GB2312ToUTF_8(string& pOut,char *pText, int pLen) { char buf[4] = {0}; int nLength = pLen* 3; char* rst = new char[nLength]; memset(rst,0,nLength); int i = 0 ,j = 0; while(i < pLen) { //如果是英文直接复制就可以 if( *(pText + i) >= 0) { rst[j++] = pText[i++]; } else { wchar_t pbuffer; Gb2312ToUnicode(&pbuffer,pText+i); UnicodeToUTF_8(buf,&pbuffer); rst[j] = buf[0]; rst[j+1] = buf[1]; rst[j+2] = buf[2]; j += 3; i += 2; } } rst[j] ='\n'; //返回结果 pOut = rst; delete []rst; return; } void UTF_8ToGB2312(char*pOut, char *pText, int pLen) { char Ctemp[4]; memset(Ctemp,0,4); int i =0 ,j = 0; while(i < pLen) { if(pText[i] >= 0) { pOut[j++] = pText[i++]; } else { WCHAR Wtemp; UTF_8ToUnicode(&Wtemp,pText + i); UnicodeToGB2312(Ctemp,Wtemp); pOut[j] = Ctemp[0]; pOut[j + 1] = Ctemp[1]; i += 3; j += 2; } } pOut[j] ='\n'; return; }后续主函数调用过程如下:
char *outStr = new[n];//根据实际需要分配// char *pText = (char *)charSymbolString.c_str(); UTF_8ToGB2312(outStr,pText,strlen(charSymbolString.c_str())); std::string strTemp = std::string(outStr);