Mac平台下数据乱码缘由

由于项目要求从文件中解析出数据而后输出,一开始练数据时什么编码格式都不知道,后来仍是拿UltraEdit一个一个试的。发现时GBK。而后输出,好蛋疼,一直乱码,因而就想着是否是须要转一下格式,百度了很久试了好多方法都没有成功。后来又想着是否是C++语言自己有固定的编码格式之类的东东,要是这样那我可怎么搞,难道把C++底层反过来玩一玩。windows

可是后来我就发现网上好多人都提到过Linux是默认使用utf-8 而windows则默认使用GBK。是否是平台的缘由呢?我用的是Mac,Mac内核是Unix的,而Linux也是Unix系的,说不定放在windows下就不会有问题啊~~而后我就把数据读出来写到数组里让我用windows的同窗输出一下,因而,我看到了我最想看到的景象。真是太坑爹了。数组


再贴上一我的家提供的方法吧,可是我没有测试成功。测试

int gbk2utf8(char *utfstr,const char *srcstr,int maxutfstrlen)编码

{spa

    if(nullptr==srcstr)3d

    {code

        printf("1bad parameter\n");orm

        return -1;unicode

    }it

    //首先先将gbk编码转换为unicode编码

    if(nullptr==setlocale(LC_ALL,"zh_CN.GBK"))//设置转换为unicode前的码,当前为gbk编码

    {

        printf("2bad parameter\n");

        return -1;

    }

    int unicodelen=(int)mbstowcs(nullptr,srcstr,0);//计算转换后的长度

    if(unicodelen<=0)

    {

        printf("can not transfer!!!\n");

        return -1;

    }

    wchar_t *unicodestr=(wchar_t *)calloc(sizeof(wchar_t),unicodelen+1);

    mbstowcs(unicodestr,srcstr,strlen(srcstr));//gbk转换为unicode

    

    //unicode编码转换为utf8编码

    if(nullptr==setlocale(LC_ALL,"zh_CN.UTF-8"))//设置unicode转换后的码,当前为utf8

    {

        printf("3bad parameter\n");

        return -1;

    }

    int utflen=(int)wcstombs(nullptr,unicodestr,0);//计算转换后的长度

    if(utflen<=0)

    {

        printf("can not transfer!!!\n");

        return -1;

    }

    else if(utflen>=maxutfstrlen)//判断空间是否足够

    {

        printf("dst str memory not enough\n");

        return -1;

    }

    wcstombs(utfstr,unicodestr,utflen);

    utfstr[utflen]=0;//添加结束符

    free(unicodestr);

    return utflen;

}

相关文章
相关标签/搜索