C++转gbk到utf mbstowcs wcstombs

2017-03-18 11:22:00
admin
原创 2445
摘要:C++转gbk到utf mbstowcs wcstombs

一、C++转gbk到utf

代码下载codeconv.cpp


#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <time.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(char *gbk, size_t *len)
{
    static char buf[1024];
    static wchar_t wbuf[1024];

    setlocale("zh_CN.gbk");
    size_t wlen = mbstowcs(wbuf, gbk, sizeof(wbuf));
    if (wlen == (size_t)-1)
        printf("mbstowcs failed.\n");

    setlocale("zh_CN.utf8");
    *len = wcstombs(buf, wbuf, sizeof(buf));
    if (*len == (size_t)-1)
        printf("wcstombs failed.\n");
    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;

    clock_t t1 = clock();
    for (int idx = 0; idx < count; ++idx)
    {
        utf = gbk2utf(gbk, &len);
    }
    clock_t t2 = clock();
    
    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);
    printf("%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大概20万左右):

./a.out 200000
CLOCKS_PER_SECOND is 1000000.
cost is 1010000, average is 0.000000.
中国

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