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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅笑一斤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值