libiconv用法详解 转gbk到utf

2017-03-18 15:11:00
admin
原创 3226
摘要:libiconv用法详解 转gbk到utf

一、libiconv用法详解

官方地址http://www.gnu.org/software/libiconv/

windows版iconv-1.9.2.win32.zip

编译

./configure --enable-static --prefix=/usr/local

make && make install

代码下载codeconv2.cpp


#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <time.h>
#include <iconv.h>

char *readFile(size_t *len)
{
    static char buf[1024];
    FILE *file = fopen("data.txt", "rb");
    *len = fread(buf, 1, 1024, file);
    fclose(file);
    return buf;
}

int setlocale(const char *loc)
{
    char *locnew;
    locnew = setlocale(LC_ALL, loc);
    if (locnew == NULL)
    {
        printf("setlocale failed.\n");
        return 0;
    }
    return 1;
}

char *gbk2utf(iconv_t ct, char *gbk, size_t *len)
{
    static char _buf[1024];
    char *inBuf = _buf;
    size_t bufLen = sizeof(inBuf);
    iconv(ct, &gbk, len, &inBuf, &bufLen);
    *len = bufLen;
    return _buf;
}

void dumpBytes(const char *bytes, size_t len)
{
    while (len--)
    {
        printf("0x%02X,", (unsigned char)*bytes++);
    }
    printf("\n");
}

void convert(int count)
{
    size_t len;
    char *gbk = readFile(&len);
    char *utf;
    iconv_t ct = iconv_open("UTF-8", "GBK");
    size_t gbkLen;

    clock_t t1 = clock();
    for (int idx = 0; idx < count; ++idx)
    {
        gbkLen = len;
        utf = gbk2utf(ct, gbk, &gbkLen);
    }
    clock_t t2 = clock();

    iconv_close(ct);
    printf("CLOCKS_PER_SECOND is %d.\n", CLOCKS_PER_SEC);
    printf("cost is %d, average is %f.\n", t2 - t1, (t2 - t1)/count/CLOCKS_PER_SEC);

    len = gbkLen;
    printf("data len is %d.\n", len);
    printf("data is %s.\n", utf);
}

int main(int argc, char **argv)
{
    int count = atoi(argv[1]);
    convert(count);

#ifdef _MSC_VER
    getchar();
#endif
    return EXIT_SUCCESS;
}


输出(转换中国,tps大概1200万左右):

./a.out 12000000
CLOCKS_PER_SECOND is 1000000.
cost is 990000, average is 0.000000.
data len is 2.
data is 中国.

发表评论
评论通过审核之后才会显示。