简体   繁体   中英

How to find two unsigned char in QByteArray

I'm pretty new to C ++ and Qt. I'm trying to check if a QByteArray shows two consecutive times a certain unsigned char. Unfortunately I am not able to make it. Does anyone know how to fix this?

QByteArray byteArray;
unsigned char start_stop = 0xFE;


if(byteArray.contains(start_stop))
{
//this one is working
}


if(byteArray.contains(start_stop+start_stop))
{
//This one not
}


if(byteArray.contains(start_stop, start_stop))
{
//This one not
}

You can arrange a function like this:

bool containsStartStop(const QByteArray & byteArray)
{
    return byteArray.contains(QByteArray(2, static_cast<char>(0xFE)));
}

or this:

bool containsStartStop(const QByteArray & byteArray)
{
    return QString(byteArray.toHex()).contains("FEFE");
}

I think this is what you are looking for, find the index of the first occurrence of two consecutive stop characters in a byte array:

#include <QtCore/QByteArray>    
#include <iostream>

int main()
{
  const char stop{'b'};
  const QByteArray v{"abcdebbaacdea"};
  std::cout << v.indexOf(QByteArray(2, stop)) << '\n'; // outputs: 5
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM