Convert hexadecimal characters to arrays and arrays to hexadecimal characters in QT

Convert hexadecimal characters to array

​
QByteArray ComService:: HexStringToByteArray(QString hex, bool *ok)
{
      int p;

      QByteArray ret;
      QStringList lst = hex.simplified().split(' ');//转化为字符串数组
      ret.resize(lst.count());
      for(int i = 0; i < lst.count(); i++)
      {
     p = lst[i].toInt(ok, 16);
     if(!(*ok) || p > 255 )
     {
         return 0;
     }
     ret[i] = p;
      }
      return ret;
}


​

 Convert array to hexadecimal characters

QString ComService:: ByteArrayToHexString(QByteArray ascii)//字符串转16进制
{
      QString ret;
      for(int i = 0; i < ascii.count(); i++)
     ret.append(QString("%1 ").arg((uchar)ascii.at(i), 2, 16, (QChar)'0'));

      return ret.toUpper();
}

Guess you like

Origin blog.csdn.net/m0_52467164/article/details/131069194