网络上不少代码都不是独立的C++代码。 要不然就是参数带了非标准C++类型的变量,
要不然干脆是其他地方自定义的类型做参数却部分拷贝出来当做开源代码。
经过搜索再进行改编,下面的C++代码可以在任何C++编译器中直接编译通过,
可以直接拷贝使用,无痛无病,一了百了,伸手可用。
////////////////////////////////////////////////////////////////////////// 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 UnicodeToGB2312(char* pOut,wchar_t uData) { WideCharToMultiByte(CP_ACP, NULL, &uData, 1, pOut, sizeof(wchar_t), NULL, NULL); }
////////////////////////////////////////////////////////////////////////// std::string UTF_8ToGB2312(char *pText, int nLen) { char * newBuf = new char[nLen+1]; char Ctemp[4]; memset(Ctemp,0,4); int i = 0; int j = 0; while(i < nLen) { 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; } } if (j <= nLen) newBuf[j] = 0; std::string strOut = newBuf; delete []newBuf; return strOut; }
|