libiconv gbk utf-8 转 unicode

81214051 2019-10-30

#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);
}

相关推荐

NewActivity / 0评论 2020-05-08