QT4.8下Ansi与UTF8的转换

#define _CUR_SYS_LINUX_ 1	//根据你的系统自己设置_CUR_SYS_LINUX_

#ifdef _CUR_SYS_LINUX_
    #include <iconv.h>
#else
    #include <windows.h>
    #include <stdio.h>
#endif

/*
unicode文件:0xff 0xfe + 内容
utf8文件:0xEF 0xBB 0xBF + 内容
无bom的utf8文件:没有前面3个字节
"回车"(carriage return) 13
"换行"(line feed)       10
Windows系统里面,每行结尾是"<回车><换行>",即"\r\n";
Unix系统里,每行结尾只有"<换行>",即"\n";
Mac系统里,每行结尾是"<回车>",即"\r"。
*/
#ifdef _CUR_SYS_LINUX_
inline int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
    iconv_t cd;
    //int rc;
    char **pin = &inbuf;
    char **pout = &outbuf;
    cd = iconv_open(to_charset,from_charset);
    if (cd==0) return -1;
    memset(outbuf,0,outlen);
    //if (iconv(cd,pin,(size_t*)(&inlen),pout,(size_t*)(&outlen))==-1) return -1;
    iconv(cd,pin,(size_t*)(&inlen),pout,(size_t*)(&outlen));
    iconv_close(cd);
    return 0;
}
#endif

/****************************************************
*utf8到ansi的转换
*输入参数:
*输出参数:
*****************************************************/
inline QByteArray Utf8ToAnsi(QByteArray &utf8)
{
#ifdef _CUR_SYS_LINUX_
    QByteArray ansi;
    char *buf = new char[utf8.size()+1];
    memset(buf,0,utf8.size()+1);
    char utf8Buf[] = {"utf-8"};
    char gb2312Buf[] = {"gb2312"};
    //ansi.resize(utf8.size());
    int code = code_convert(utf8Buf,gb2312Buf,utf8.data(),utf8.size(),buf,utf8.size());
    if (code == -1) {
        delete []buf;
        ansi = "";
        //qDebug() << "Utf8ToAnsi error";
        return ansi;
    }
    ansi = buf;
    qDebug() << "utf8 length=" << utf8.size();
    qDebug() << "ansi length=" << ansi.size();
    delete []buf;
    return ansi;
#else
    int len;
    QByteArray result;
    //UTF-8转UNICODE
    len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)utf8.data(), -1, NULL,0);
    WCHAR * unicode = new WCHAR[len+1];
    memset(unicode, 0, len * 2 + 2);
    MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)utf8.data(), -1, unicode, len);
 
    //UNICODE转ANSI
    len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL);
    char *ansi=new char[len + 1];
    memset(ansi, 0, len + 1);
    WideCharToMultiByte (CP_ACP, 0, unicode, -1, ansi, len, NULL,NULL);
 
    //赋值
    result = ansi;
    delete[] unicode;
    delete[] ansi;
    return result;
#endif
}
 
/****************************************************
*ansi到utf8的转换
*输入参数:
*输出参数:
*****************************************************/
inline QByteArray AnsiToUtf8(QByteArray &ansi)
{
#ifdef _CUR_SYS_LINUX_
    QByteArray utf8;
    char *buf = new char[ansi.size()*2];
    char utf8Buf[] = {"utf-8"};
    char gb2312Buf[] = {"gb2312"};
    //ansi.resize(ansi.size()*2);
    int code = code_convert(gb2312Buf,utf8Buf,ansi.data(),ansi.size(),buf,2*ansi.size());
    if (code == -1) {
        delete []buf;
        utf8 = "";
        return utf8;
    }
    utf8 = buf;
    delete []buf;
    return utf8;
#else
    int len;
    QByteArray result;
    //ANSI转UNICODE
    len = MultiByteToWideChar(CP_ACP, NULL, ansi.data(), -1, NULL, 0);
    WCHAR * unicode = new WCHAR[len+1];
    memset(unicode, 0, len * 2 + 2);
    MultiByteToWideChar(CP_ACP, NULL, ansi.data(), -1, unicode, len);
 
    //UNICODE转utf8
    len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
    char *utf8=new char[len + 1];
    memset(utf8, 0, len + 1);
    WideCharToMultiByte (CP_UTF8, 0, unicode, -1, utf8, len, NULL,NULL);
 
    //赋值
    result = utf8;
    delete[] unicode;
    delete[] utf8;
    return result;
#endif
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值