请问如何将一个字符串从GBK转到UTF-8?

setimouse 2011-10-25 12:43:30
请问Objective-C中如何进行字符串编码转换?
对于GBK的网页,希望将其内容转换成UTF-8。
PHP中处理非常简单,但是Objective-C里却一直找不到办法。

请问有没有办法来进行转码呢?

谢谢!
...全文
117 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cocos2der 2011-10-29
  • 打赏
  • 举报
回复
调用:GBKToUTF8(str对象, “gb2312”, “utf-8”);

int GBKToUTF8(std::string & gbkStr, const char* toCode, const char* fromCode)
{

iconv_t iconvH;
iconvH = iconv_open(fromCode, toCode);
if (iconvH == 0)
{
return -1;
}

const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*) malloc(strLength*4);
char* pBuff = outbuf;

memset( outbuf, 0, strLength*4);
size_t outLength = strLength*4;
if (-1 == iconv(iconvH, pin, &strLength, &outbuf, &outLength))
{
iconv_close(iconvH);
return -1;
}
gbkStr = pBuff;
iconv_close(iconvH);
return 0;
}

你试试

29,031

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧