在Linux下,可以调用库,代码如下:
bool CXMLAnalysis::UTF8_2_GB2312(char *in, int inLen, char *out, int outLen)
{
#ifndef WIN32
iconv_t cd = iconv_open( "gb2312", "UTF-8" ); //ANSI_X3.110-1983/gb2312
// check cd
if( (int)cd == -1 )
{
cout << "iconv is ERROR" << endl;
return false;
}
printf("\niconv return value is: %d \n", cd );
char *pin = in, *pout = out;
int inLen_ = inLen + 1;
int outLen_ = outLen;
iconv( cd, &pin, (size_t*)&inLen_, &pout, (size_t*)&outLen_ );
// check iconv
printf("\nin is:%s, out is:%s",pin, pout);
printf("\nnumber in is:%d , out is:%d", inLen_, outLen_);
iconv_close(cd);
#endif
return true;
}
___________________________________________
在Windows下可以调用
BOOL CPlayAVIDlg::StrToUTF_8(char *pstr, CString& strUTF_8)
{
// int nStrLen = strlen(pstr);
int nLenUnicode = MultiByteToWideChar(CP_ACP,MB_COMPOSITE,pstr,2,NULL,0);
if(nLenUnicode == 0) return FALSE;
WORD* pUnicode = new WORD[nLenUnicode];
if (pUnicode==NULL) return FALSE;
//ASCII转换成Unicode
nLenUnicode = MultiByteToWideChar(CP_ACP,0,pstr,2,pUnicode,nLenUnicode);
if(nLenUnicode == 0) return FALSE;
BYTE* pByteRet = (BYTE*)pUnicode;
//Unicode转换成UTF-8 //CP_UTF8
char dest[6] = "";
nLenUnicode = WideCharToMultiByte(CP_UTF8, 0, pUnicode, nLenUnicode, dest, 6, NULL, NULL );
CString strDest("");
for(int i=0; i {
char szBuff[10] = {'\0'};
itoa((int)dest[i], szBuff, 16);
szBuff[8]='\0';
CString strTemp(&szBuff[6]);
strDest += strTemp;
}
strDest +=";";
//strDest.Format("%S%X%X;", (BYTE)dest[0],(BYTE)dest[1],(BYTE)dest[2]);
//strDest = dest;
strUTF_8 = strDest;
delete[] pUnicode;
return TRUE;
}
--------------------next---------------------
阅读(283) | 评论(0) | 转发(0) |