高手进 关于GBK-2312和UTF8之间的字符相互转化

CJBAAA 2011-01-14 02:06:56

用C/C++库函数怎样实现关于GBK-2312和UTF8之间的字符串相互转化,如果有源码更好,提供相应库函数也非常感谢。
...全文
193 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rwjlqn 2011-01-14
  • 打赏
  • 举报
回复
gbk<--->unicode<---->utf8
iambic 2011-01-14
  • 打赏
  • 举报
回复
自己google下。
另外听说过gbk和gb2312,GBK-2312这种叫法就很不正经。
Coder李海波 2011-01-14
  • 打赏
  • 举报
回复
参考一下吧
#include <iconv.h>
#include <errno.h>

void Encoding::Open() {
cd_ = iconv_open(to_charset_, from_charset_); //from_charset_="GBK" to_charset_="UTF-8"
if (cd_ == (iconv_t) (-1)) {
printf(
"The conversion from %s to %s is not supported by the implementation.",
from_charset_, to_charset_);
cd_ = NULL;
}
}

int Encoding::ConvertCharset(char* inBuf, int inBufLen, char* outBuf,
int outBufLen) {
if (cd_ == NULL) {
printf("Please init iconv first.\n");
return -1;
}
if(inBuf == NULL || inBufLen == 0)
return 0;
if(outBuf == NULL || outBufLen == 0)
return 0;

char* tmpbuff = outBuf;
int tmplen = outBufLen;

size_t size = iconv(cd_, &inBuf, (size_t *) &inBufLen, &tmpbuff,
(size_t *) &tmplen);
if (size == (size_t) (-1)) {
if (errno == EILSEQ) {
printf(
"An invalid multibyte sequence has been encountered in the input.");
} else if (errno == E2BIG) {
printf("There is not sufficient room at *outbuf.");
} else if (errno == EINVAL) {
printf(
"An incomplete multibyte sequence has been encountered in the input.");
}
return -1;
}
return outBufLen - tmplen - 1;
}
xunmengren2009 2011-01-14
  • 打赏
  • 举报
回复
GNU libiconv 编码转换库

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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