码迷,mamicode.com
首页 > 其他好文 > 详细

libiconv gbk utf-8 转 unicode

时间:2019-10-30 18:24:51      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:for   short   efi   eof   lib   break   size   hello   const   

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "iconv.h"

#define CODE_UNICODE "UCS-2LE"

int code_convert(const char *pFromCharset, const char *pToCharset, char *pInBuf, size_t nInLen, char *pOutBuf, size_t nOutLen)
{
    iconv_t cd;
    int rc;
    /* 防止指针偏移. */
    char **pin = &pInBuf;
    char **pout = &pOutBuf;

    cd = iconv_open(pToCharset, pFromCharset);
    if (cd == 0) {
        printf("iconv_open err! %s to %s\n", pFromCharset, pToCharset);
        return -1;
    }
    memset(pOutBuf, 0, nOutLen);
    if (iconv(cd, pin, &nInLen, pout, &nOutLen) == -1)
    {
        printf("iconv err! %s to %s\n", pFromCharset, pToCharset);
        return -2;
    }    
    iconv_close(cd);

    return 0;
}

int codeToConvert(const char *pFromCharset, const char *pToCharset, char *pInBuf, int nInLen, char *pOutBuf, int nOutLen)
{
    char *tmpBuf = (char*)malloc(nOutLen);

    if (!tmpBuf)
    {
        printf("invalid params!\n");
        return -1;
    }
    int nRet = code_convert(pFromCharset, pToCharset, pInBuf, nInLen, tmpBuf, nOutLen);
    if (0 == nRet)
    {
        memset(pOutBuf, 0, nOutLen);
        for (int i = 0; i < nOutLen; ++i)
        {
            if (i % 2 == 0 && *(short*)(tmpBuf + i) == 0)
                break;
            sprintf(pOutBuf + 2 * i, "%02x", tmpBuf[i] & 0xff);
        }
    }
    free(tmpBuf);
    tmpBuf = NULL;

    return nRet;
}

int utf8ToUnicode(char *pInBuf, int nInLen, char *pOutBuf, int nOutLen)
{
    return codeToConvert("UTF-8", CODE_UNICODE , pInBuf, nInLen, pOutBuf, nOutLen);
}

int gbkToUnicode(char *pInBuf, int nInLen, char *pOutBuf, int nOutLen)
{
    return codeToConvert("GBK", CODE_UNICODE, pInBuf, nInLen, pOutBuf, nOutLen);
}

int main()
{
    char inbuf[] = "欢迎你好,hello world!";
    char outbuf[128] = { 0 };

    utf8ToUnicode(inbuf, strlen(inbuf), outbuf, sizeof(outbuf));
    printf("utf-8 to unicode out: %s\n", outbuf);

    gbkToUnicode(inbuf, strlen(inbuf), outbuf, sizeof(outbuf));
    printf("gbk to unicode out: %s\n", outbuf);
}

 

libiconv gbk utf-8 转 unicode

标签:for   short   efi   eof   lib   break   size   hello   const   

原文地址:https://www.cnblogs.com/zbs337650377/p/11766183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!