qt字符编码及数据格式转换
1.字符编码
QString:
QString内部可能是使用unicode字符集来存储文字,UTF-8以字节为单位对Unicode进行编码。QString里面的汉字是UTF-8编码的字符集,QString::toUtf8是输出UTF-8编码的字符集
Latin1:
Latin1代表ASCII,QString::toLatin1是相当与ASCii码不包含中文的遇到中文默认转换为ascii码,"汉字"不在latin1字符集中,所以结果无意义
Local8Bit:
Local8Bit代表unicode 统一码
ASCII、Unicode、UTF8区别:
ASCII编码是1个字节,而Unicode编码通常是2个字节。2、ASCII是单字节编码,无法用来表示中文;而Unicode可以表示所有语言。3、用Unicode编码比ASCII编码需要多一倍的存储空间
UTF-8编码把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节。
在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。用记事本编辑的时候,从文件读取的UTF-8字符被转换为Unicode字符到内存里,编辑完成后,保存的时候再把Unicode转换为UTF-8保存到文件。
*** msvc编译器,默认汉字使用gb18030字符集 ***
2. ByteArray 与 int 的 互转
2.1 ByteArray 转 Int
int CtLabelAndEdit::byteArrayToInt(const QByteArray &data, int endian)
{int count = data.length();Q_ASSERT(count <= static_cast<qint32>(sizeof(int)));int nRet = 0;qint8* point = reinterpret_cast<qint8 *>(&nRet);if(endian == 1)//小端模式{for (int i = count - 1; i > -1; --i){*point = data.at(i);point++;}}else //大端模式{for (int i = 0; i < count; ++i){*point = data.at(i);point++;}}return nRet;
}
2.2 int 转 ByteArray
void CtLabelAndEdit::addIntToByteArray(QByteArray &command, int data, int count, int endian = 0)
{if(endian == 1) //小端模式(低序字节存储在起始地址){for (int i = count - 1; i > -1; --i){command.append(static_cast<qint8>((data >> (8 * i)) & 0xFF));}}else //大端模式(高序字节存储在起始地址){for (int i = 0; i < count; ++i){command.append(static_cast<qint8>((data >> (8 * i)) & 0xFF));}}
}
2.3 qint8 转 ByteArray
QByteArray data;data.append(static_cast<qint8>(m_pCombo->currentIndex()));
3. ByteArray 与 QString(Hex字符串) 的 互转
3.1 QString(Hex字符串) 转 ByteArray
bool FFDataManager::translateHexStringToByteArray(const QString &src, QByteArray &des)
{QString temp = src;temp.replace(" ", "");int length = temp.length();if (length % 2 != 0){return false;} else {des.clear();for (int i = 0; i < length; i += 2){des.append(static_cast<char>(temp.mid(i, 2).toUShort(Q_NULLPTR, 16)));}}return true;
}
3.2 ByteArray 转 QString(Hex字符串)
QByteArray da;
QString str = QString(da.toHex());
4. ByteArray 与 QString(文本字符串) 的 互转
4.1 QString(字符串) 转 ByteArray
QString str("hello");
QByteArray bytes = str.toUtf8();
或
QString str("hello");
QByteArray bytes = str.toLatin1();
4.2 ByteArray 转 QString(字符串)
QByteArray bytes("hello world");
QString string = bytes;
或
QByteArray bytes("hello world");
QString string;
string.prepend(bytes);
5. char* 与 QString(文本字符串) 的 互转
5.1 QByteArray 转换为 char *
char *ch;//不要定义成ch[n];
QByteArray byte;
ch = byte.data();
5.2 char * 转换为 QByteArray
char *ch;
QByteArray byte;
byte = QByteArray(ch);
6. 进制转换
6.1 十进制转十六进制
int dec = 20;qDebug() << QString("%1").arg(dec, 4, 16, QLatin1Char('0')); // 0014
6.2 十进制转二进制
qDebug() << QString("%1").arg(dec, 4, 2, QLatin1Char('0')); // 10100
7. hex()与fromhex()
bytes对象的hex函数,用来将bytes对象的值转换成hex字符串;而fromhex函数,用来将hex字符串导入bytes对象,相当于用hexstr来创建bytes对象
QByteArray bytearray = QByteArray::fromHex("1a2b3c4d5e6f");
byteArray.toHex(":"); // return "1a:2b:3c:4d:5e:6f"
byteArray.toHex(0); // return "1a2b3c4d5e6f"
byteArray.toHex('\0'); // return "1a2b3c4d5e6f"
8. std::string转QString
string s = "hello, world"QString ss = QString::fromStdString(s);qDebug()<<"ss = "<< ss;
9. std::wstring转QString
std::wstring wstr(L"你好, 世界");QString ss = QString::fromStdWString(wstr);qDebug()<<"ss = "<< ss;
qt字符编码及数据格式转换相关推荐
- (转载)Python常见字符编码间的转换
Python常见字符编码间的转换 主要内容: 1.Unicode 和 UTF-8的爱恨纠葛 2.字符在硬盘上的存储 3.编码的转换 4.验证编码是否转换正确 5 ...
- python转换字符编码_Python常见字符编码间的转换
学习Python,字符编码间的转换是绕不过去的一只拦路虎,不把编码彻底搞明白,总有一天它会猝不及防坑你一把. Python2.x和Python3.x在字符编码的设置上也有很大区别(Python3未来将 ...
- Qt 字符编码转换(UTF-8 转换为 GBK) \u7528\u6237\u672a\u7b7e\u7ea6
UTF-8 转换为 GBK \u7528\u6237\u672a\u7b7e\u7ea6 编码知识(\u7528\u6237\等的就是Unicode编码) 转换编码示例 编码知识(\u7528\u62 ...
- Qt字符编码,创建中文文件
1.代码文件本身编码: 2.Qt中用于控制读入和写出文件系统时的字符编码由QTextCodec::setCodecForLocale()所决定.一般不用设置,Qt跟系统交互时会根据系统的Locale来 ...
- Python常见字符编码间的转换教程
1. 前言 Python2.x和Python3.x在字符编码的设置上也有很大区别(Python3未来将是主流,所以Python3为主),今天我们就来一起学习下. 2. Unicode 和 UTF-8的 ...
- ASP.NET下QueryString不同字符编码间强制转换的解决方案
正常的情况下,现在asp.net的网站很多都直接使用UTF8来进行页面编码的,这与Javascript.缺省网站的编码是相同的,但是也有相当一部分采用GB2312. 对于GB2312的网站如 ...
- iconv 判断字符编码_iconv 字符集转换报错
iconv 字符集转换出错 本帖最后由 zw91683 于 2014-02-26 15:59:48 编辑 最近有个项目在用字符集转换,网上查了下iconv的用法,直接拿过来用,发现运行一直出错,代码如 ...
- Python中的字符串与字符编码:编码和转换问题
原文转载自:http://www.cnblogs.com/yyds/p/6171340.html 读后感:最近在跑实验,对于中文编码问题一直感到困扰,读完这篇文章以后,了解了Unicode编码的一些信 ...
- python转换字符编码_转:Python常见字符编码及其之间的转换
一.Python常见字符编码 字符编码的常用种类介绍 第一种:ASCII码 ASCII(American Standard Code for Information Interchange,美国信息交 ...
最新文章
- linux查看内核版本、系统版本、系统位数(32or64)
- MFC类中获得其它类指针
- nfs服务器与客户端配置
- H5网页播放器播不了服务器上的mp4视频文件
- 查看目标主机安装的杀毒软件
- 内镜手术机器人行业调研报告 - 市场现状分析与发展前景预测
- python_thrift
- 2017.3.27-morning
- (附源码)springboot高校科研管理系统 毕业设计 222055
- 【LaTeX入门】软件安装
- 【算法】基于AOE网的关键路径算法
- have datatype/md5sum 相关的错误 IndentationError: unexpected indent
- 英国“脱欧”促中国企业调整在欧战略
- python如何返回一个列表_python如何返回元组,列表或字典的?
- 什么?还在用delete删除数据《死磕MySQL系列 九》
- ToLua原理和使用教程
- 静态数码管显示数字及字母
- 2023最新二开华夏基金投资理财源码+带客服功能/前端UI全部重构
- OpenGL 图形变换
- 2020德勤面试开始了吗_2020秋招,德勤群面par面,面试小白怎么准备?