1.项目需求:GB2312转UTF编码

2.思路:iconv库的使用

3.代码实例

/************************************************
*   @FileName     : gb2utf.c
*   @Description  : Code convert
*   @Editor       : Donkey
*   @Date         : 2018-12-01  13:07
*************************************************/
#include "UtfGb.h"

int code_convert(char *from_charset,char *to_charset,char *inbuf,ssize_t inlen,char *outbuf,ssize_t outlen)
{
    iconv_t cd;
    char **pin = &inbuf;
    char **pout = &outbuf;
    
    cd = iconv_open(to_charset,from_charset);
    if (cd==0) return -1;
    memset(outbuf,0,outlen);
    if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
    iconv_close(cd);
    return 0;
}

int g2u(char * inbuf, int inlen, char* outbuf, int outlen){
    return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}

int main(int argc, const char * argv[]) {
  char in[] = {0xD4,0xB6,0xB3,0xCC,0xBF,0xAA,0xCB,0xF8};
  char buf[20]={0};
  int len = 20;
  int l=8;
  g2u(in, l, buf, len);

  printf("%s\n",buf);

  return 0;
}

4.输出

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐