tuf8和gb2312编码转换

在这里插入图片描述
qt界面默认是utf8,Vs是gb2312,所以会有乱码,所以在输入时,将gb2312转换为utf8,用setWindowsTitle示例,直接输入中文,qt界面就会乱码,经过转换后的返回值输入就不会再乱码了

#include  <qtextcodec.h>
#include <string>
构造函数
{
    ui.setupUi(this);

std::string temp("模板方法");

    char str[10] = {0};
   setWindowTitle(TemplateMethod::Gb2312_utf8(temp, str));
}

char *  TemplateMethod::Gb2312_utf8(std::string strutf8, char* str)
{
    //头文件QTextCodec
    QTextCodec* utf8Codec = QTextCodec::codecForName("utf-8");
    QTextCodec* gb2312Codec = QTextCodec::codecForName("gb2312");

    QString strUnicode = gb2312Codec->toUnicode(strutf8.c_str());
    QByteArray ByteUtf8 = utf8Codec->fromUnicode(strUnicode);

    strcpy(str, ByteUtf8.data());
    return str;
}

char* TemplateMethod::utf8_Gb2312(std::string strgb2312, char* str)
{
    QTextCodec* utf8Codec = QTextCodec::codecForName("utf-8");
    QTextCodec* gb2312Codec = QTextCodec::codecForName("gb2312");

    QString strUnicode = utf8Codec->toUnicode(strgb2312.c_str());
    QByteArray ByteGb2312 = gb2312Codec->fromUnicode(strUnicode);

    strcpy(str, ByteGb2312.data());
    return str;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值