- 論壇徽章:
- 0
|
#include <iconv.h>
#include <iostream>
#include <stdio.h>
using namespace std;
bool UTF_8ToGB2312(char* pDst, char* pSrc)
{
iconv_t conv;
size_t uSrcLen;
size_t uDstLen;
//strerror(errno)的值,當(dāng)字符集名稱全為大寫時(shí)是Invalid wide character,否則為Invalid argument
conv = iconv_open("GBK","UTF-8");
if(conv == (iconv_t)-1)
{
return false;
}
uSrcLen = strlen(pSrc);
uDstLen = 200;
memset(pDst, 0x00, uDstLen);
if (-1 == iconv(conv, &pSrc, &uSrcLen, &pDst, &uDstLen))
{
iconv_close(conv);
return false;
}
iconv_close(conv);
return true;
}
int main()
{
char *strUtf = "璋冪敤BOSS緋葷粺寮傚父錛岃?閲嶆柊灝濊瘯銆?";
char strGb[200];
if (UTF_8ToGB2312(strGb, strUtf))
{
cout<<strGb<<endl;
}
else
{
cout<<"failed"<<endl;
}
return 0;
}
用iconv -l查看,GBK和UTF-8都是有的。
另外,用命令行iconv -f UTF-8 -t GB2312 my.txt,是可以轉(zhuǎn)換成功的。
誰(shuí)遇過(guò)類似的問(wèn)題,大家?guī)蛶兔Γ?br />
[ 本帖最后由 lingat242 于 2009-7-20 10:10 編輯 ] |
|