前几天接了个任务做POP3的邮件编码转换,本来感觉是很简单的事情,但是总共还是花了三天时间才做到自己比较满意的效果。

简单记录下遇到的几个坑,也许对别人有帮助。

1、入参改变的问题

iconv的函数定义如下:

#include

size_t iconv(iconv_t cd,

char **inbuf, size_t *inbytesleft,

char **outbuf, size_t *outbytesleft);

看下man里面的说明,可以通过man 3 iconv获取,简单翻译下重点,就是在转换的时候会逐个字节读取inbuf的值,并会对其值做改变,同时减少Inbyteleft字节数,将结果写入到outbuf中,增加outbuf,outbytesleft逐渐减小,入参的改变要特别小心,如果你的outbuf要释放,就不能直接传递进去,因为一旦发生了改变,free()的时候就会core掉,所以一般都是传递临时指针给它们。

The iconv() function converts one multibyte character at a time, and for each character conversion it increments *inbuf and decrements *inbytesleft by the num‐

ber of converted input bytes, it increments *outbuf and decrements *outbytesleft by the number of converted output bytes, and it updates the conversion state

contained in cd. If the character encoding of the input is stateful, the iconv() function can also convert a sequence of input bytes to an update to the con‐

version state without producing any output bytes; such input is called a shift sequence. The conversion can stop for four reasons:

1. An invalid multibyte sequence is encountered in the input. In this case it sets errno to EILSEQ and returns (size_t) -1. *inbuf is left pointing to the

beginning of the invalid multibyte sequence.

2. The input byte sequence has been entirely converted, that is, *inbytesleft has gone down to 0. In this case iconv() returns the number of nonreversible

conversions performed during this call.

3. An incomplete multibyte sequence is encountered in the input, and the input byte sequence terminates after it. In this case it sets errno to EINVAL and

returns (size_t) -1. *inbuf is left pointing to the beginning of the incomplete multibyte sequence.

4. The output buffer has no more room for the next converted character. In this case it sets errno to E2BIG and returns (size_t) -1.

2、类型的问题

传入参数的数据类型一定要是对的,不能做强制转换,不然可能会发生core的问题,我就遇到过一次。

3、例子

#include

#include

#include

#include

static int charset_convert(char *inbuf,size_t inlen,char *outbuf,size_t * poutlen,char * from_charset,char * to_charset)

{

iconv_t cd;

char **pin = &inbuf;

char **pout = &outbuf;

int outlen = * poutlen;

cd = iconv_open(to_charset,from_charset);

if (cd == (iconv_t)-1) {

perror("open error");

return -1;

}

memset(outbuf,0,outlen);

if (iconv(cd,pin,&inlen,pout,poutlen) == -1) {

perror("Iconv error");

}

iconv_close(cd);

return 0;

}

int main(void)

{

int r;

char *sin, *sout;

size_t lenin, lenout;

char src []= {0xc3,0xf7,0xd2,0xed,0x00};

char dst[16] = {0};

iconv_t c_pt;

sin = src;

lenin = strlen(src);

sout = dst;

lenout = 16;

printf("len:%d\n",lenin);

charset_convert(src,lenin,dst,&lenout,"GB18030","UTF-8");

printf("start:%d len:%d SRC[%s], DST[%s].\n",lenin,lenout, src, dst);

return 0;

}

c语言iconv转码错误,调用iconv转换编码坑相关推荐

  1. c语言iconv转码错误,iconv编码转换的问题

    在网上有如下代码: /*代码转换:从一种编码转为另一种编码*/ int code_convert(const char *from_charset, const char *to_charset, c ...

  2. php iconv detected,PHP错误:iconv() Detected an illegal character

    @header("Content-type:text/html;charset=GB2312"); $arr = array ('name'=>"贾朝藤" ...

  3. C#语言实例源码系列-调用OutLokk发送邮件

    专栏分享 点击跳转=>Unity3D特效百例 点击跳转=>案例项目实战源码 点击跳转=>游戏脚本-辅助自动化 点击跳转=>Android控件全解手册

  4. c语言文书源码,[应用文书]c 计算机编码.doc

    [应用文书]c 计算机编码 编码定义 ?? ?? 在计算机硬件中,编码(coding)是在一个主题或单元上为数据存储,管理和分析的目的而转换信息为编码值(典型地如数字)的过程.在软件中,编码意味着逻辑 ...

  5. 【C语言】BCD码、十进制互相转换

    目录 0. 前言: 1. BCD码 2. 算法原理 3. 进制转换 3.1 两位BCD码的转换: 3.2 其他进制转换 3.3 任意进制转二进制 参考资料: 0. 前言: 记录今天用15单片机写DS1 ...

  6. C#语言实例源码系列-实现Word转换RTF

    专栏分享 点击跳转=>Unity3D特效百例 点击跳转=>案例项目实战源码 点击跳转=>游戏脚本-辅助自动化 点击跳转=>Android控件全解手册

  7. C#语言实例源码系列-实现Word转换TXT

    专栏分享 点击跳转=>Unity3D特效百例 点击跳转=>案例项目实战源码 点击跳转=>游戏脚本-辅助自动化 点击跳转=>Android控件全解手册

  8. 初始C语言——利用Ascll码进行字母大小写转换

    打开Ascll码表,你会发现大写字母和小写字母之间存在这样的关系. 图片来自:https://img-blog.csdnimg.cn/54404234b42348d6a33bc1c4d5ab24e5. ...

  9. cocos2dx中使用iconv转码(win32,iOS,Android)

    首先贴下环境:Win7 64, NDK r8e, libiconv-1.14, cygwin 一 Win32环境配置 Cocos2D-X自带有win32上的iconv库.仅仅须要配置一下就可以使用. ...

最新文章

  1. UIView淡入淡出动画
  2. C++中的运算符重载
  3. Poisson Image Editing 泊松融合 matlab代码完整
  4. 测试你的杀毒软件实时监控能力!
  5. 全栈性能测试修炼宝典jmeter实战电子版_推荐一款技术人必备的接口测试神器:Apifox...
  6. LuaForUnity8:uLua简介
  7. SEEDLab DNS_Remote Attack 实验报告
  8. java中5 的结果_java第五次实验报告
  9. Javascript实现全屏阅读和复制功能
  10. 移动硬盘某个分区打不开,显示“文件或目录损坏且无法读取”的解决方法
  11. python读取csv文件表头_Python读取CSV文件
  12. 低速接口之SPI接口,分类,四种模式,特点
  13. Soo 防环机制, 在配置了之后如果show 出来还是没有生效,建议clear 一下bgp,最好实验就是 把 物理接口给 shutdown--在no shutdown ,就好了,记住,!!!
  14. 金融系统中PBOC/EMV的TLV的算法实现(含C++/C#)
  15. 翻转课堂---案例1
  16. 微信H5资源预加载(图片、字体)
  17. 买房的流程和中间的坑
  18. 饼图legend显示百分比
  19. 在ubuntu添加中文打字法
  20. chatgpt把脉知识星球

热门文章

  1. Python游戏(2) —— 发牌游戏
  2. 图数据库(neo4j)学习心得
  3. 解决Docker容器没有权限写入宿主机目录
  4. MATLAB打开,中文注释乱码情况怎么解决?
  5. 【笔记】游戏制作技巧——3
  6. Spark一路火花带闪电——Spark踩坑记
  7. Swift快速入门(三)运算符
  8. iphone html5 跑多少钱,2018款新iPhone售价曝光:起步价维持去年水平
  9. java从服务器读取文件_Java从服务器读取文件并下载到本地
  10. 完整电商项目--(五)用户基本信息(2):收货地址