HEXString和QByteArray转换

HEXString和QByteArray转换

参考文档:https://blog.csdn.net/weixin_42837024/article/details/81511304

  • 1.HEXString 转换QByteArray

QByteArray EncryOpera::HexStringToByteArray(QString HexString)
{
    bool ok;
    QByteArray ret;
    HexString = HexString.trimmed();          //去除开始和结束的空格
    HexString = HexString.simplified();       //同上,并且将内部空格变成单个空格-也包括清楚ASCII码字符
    QStringList sl = HexString.split(" ");    //通过split(" ")空格分割String

    foreach (QString s, sl) {
        if(!s.isEmpty())
        {
            char c = s.toInt(&ok,16)&0xFF;
            if(ok){
                ret.append(c);
            }else{
                qDebug()<<"Illegal hexadecimal character:"<
  • 2.QByteArray 转换 HEXString

//QByteArray  -> HEXQString
QString EncryOpera::ByteArrayToHexString(QByteArray data){
    QString ret(data.toHex().toUpper());
    int len = ret.length()/2;
    for(int i=1;i
  • 3.HEXString(无空格) 转换 HEXString(有空格)

//HEXQString(无空格)  ->  HEXQString(有空格)
QString EncryOpera::StringNONullToNull(QString hexString)
{
    QString ret = hexString;
    int len = hexString.length()/2;
    for(int i=1;i

你可能感兴趣的:(QT,C++)