Qt:TXT文本读写的字符编码

Response返回我是这样写的,一开始写的toLocal8Bit(),在浏览器地址里面输入127.0.0.1:8080,能正常

显示中文,但web处理的时候却乱码了。然后我将toLocal8Bit()修改为toUtf8(),web就能正常处理了

复制代码
void HttpController::service(HttpRequest &request, HttpResponse &response) {

    QFile aFile("xxxxx.txt");

    if(!aFile.exists())
    {
        response.write("File does not exist",false);
    }

    if(!aFile.open(QIODevice::ReadOnly |QIODevice::Text))
    {
        response.write("Pattern exception",false);
    }

    QTextStream aStream(&aFile);
    QString text;
    text = aStream.readAll();
    //QByteArray byte= text.toLocal8Bit();
    QByteArray byte = text.toUtf8();
    response.write(byte,true);
    aFile.close();

}

toUtf8是输出UTF-8编码的字符集

toLatin1是相当与ASCii码不包含中文的遇到中文默认转换为ascii码0x3f也就是字符'?'

Local8bit是本地操作系统设置的字符集编码,一般为GB2312.

相关推荐
TH_11 天前
腾讯云-(1)-轻量级服务器购买
服务器·云计算·腾讯云
山卡拉噶1 天前
在Linux中安装Kdump调试环境
linux·运维·服务器
扶尔魔ocy1 天前
【QT opencv】手动去噪--网格化获取区域坐标
开发语言·qt·opencv
q***72561 天前
【JOIN】关键字在MySql中的详细使用
数据库·mysql
萤火夜1 天前
MYSQL之事务
数据库·mysql
q***R3081 天前
MySQL并发
数据库·mysql
星辰_mya1 天前
浅谈redis中的hash
数据库·redis·哈希算法
TT哇1 天前
【计算机网络】经典易错题 1.概述 2 物理层 3.数据链路层 4.网络层
android·服务器·计算机网络
Hat_man_1 天前
虚拟机Ubuntu22.04交叉编译Qt5.15.2(ARM64)
开发语言·qt
人工智能训练1 天前
在Windows系统Docker中使用wsl2、容器、windows文件路径三种不同挂载方式的区别和性能差异
运维·服务器·人工智能·windows·docker·容器·wsl2