linux c将缓存字符串进行编码转换


#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iconv.h>
#include <errno.h>
#include <string.h>
	
iconv_t charset_init(char *tocharset,char *fromcharset)
{
    iconv_t charst;
    if(NULL == fromcharset || NULL == tocharset ||
       0 == strlen(fromcharset) || 0 == strlen(tocharset))
        return NULL;

    charst = iconv_open(tocharset, fromcharset);
    if((iconv_t)-1 == charst)
    {
        printf("iconv open error %s\n",strerror(errno));
        return (iconv_t)-1;
    }
    return charst;
}

void charset_close(iconv_t charst)
{
    if(charst < 0)
       return;
    iconv_close(charst);
    return;
}

void change_charset(iconv_t charst,char *inbuf,size_t *inlen,char *outbuf,size_t *olen)
{
    size_t re;

    re = iconv(charst, &inbuf,inlen,&outbuf,olen);
    if(-1 == re)
    {
        printf("iconv error %s\n",strerror(errno));
        return;
    }
    return;
}

/*
使用以下方式将缓存中字符串换成另一种编码字符输出
charst = charset_init(tocharset,fromcharset);
if(-1 == charst)
   ...
change_charset(charst, inbuf,&inlen,outbuf,&outlen);
charset_close(charst);
*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值