Chinaunix首页 | 论坛 | 博客
  • 博客访问: 578200
  • 博文数量: 752
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(752)

文章存档

2011年(1)

2008年(751)

我的朋友

分类:

2008-10-13 16:51:18

在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("&#x");
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("&#x%S%X%X;", (BYTE)dest[0],(BYTE)dest[1],(BYTE)dest[2]);
//strDest = dest;
strUTF_8 = strDest;

delete[] pUnicode;
return TRUE;
}

--------------------next---------------------

阅读(275) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~