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.

相关推荐
山峰哥4 分钟前
SQL优化中的索引策略与Explain分析实战
大数据·汇编·数据库·sql·编辑器
galaxyffang9 分钟前
Redis 的 16 个数据库应用场景
数据库·redis·缓存
喜欢猪猪10 分钟前
深度解析 SGLang:大模型编程新范式——从 Prompt Engineering 到 Structured Generation 的系统性跃迁
java·数据库·prompt
·云扬·14 分钟前
利用Orchestrator Hook实现MySQL高可用切换与VIP管理
android·数据库·mysql
Xの哲學1 小时前
Linux SLUB 内存分配器深度剖析: 从设计哲学到实战调试
linux·服务器·网络·算法·边缘计算
何中应1 小时前
linux使用root账户操作提示没有权限
linux·运维·服务器
天上飞的粉红小猪1 小时前
网络基础概念
linux·服务器·网络·c++
KingRumn2 小时前
DBUS源码剖析之DBusMessage消息头
linux·服务器·算法
YIN_尹2 小时前
【MySQL】数据库基础
数据库·mysql·adb
秃狼2 小时前
mysql explain 使用入门
数据库·mysql