Solved qbytearray find two similar values
-
hi i have data like this
"8800010026020000086A54161565415621" + some another data + "8800010026020000083A352303661445"
i want to find the two index start with "8800010026020000"QFile file("somthing"); file.open(QIODevice::ReadOnly); QByteArray data = file.readAll(); file.close(); QByteArray patch_data1 = QByteArray::fromHex("8800010026020000"); int64_t index = data.indexOf(patch_data1); qInfo() <<data.mid(index,16).toHex();
-
@fadu
qsizetype QByteArray::indexOf(QByteArrayView bv, qsizetype from = 0) const takes an optional second parameter where you tell it to start the search from. -
@fadu
You have the first match. So go ahead and search after that for the second match, is that what you mean? -
@JonB
yes that's what i mean but it's return the first value only -
@fadu
qsizetype QByteArray::indexOf(QByteArrayView bv, qsizetype from = 0) const takes an optional second parameter where you tell it to start the search from. -
@JonB
that's it thanks a lot
the problem is solved and this is the new codeQFile file("test.qcn"); file.open(QIODevice::ReadOnly); QByteArray data = file.readAll(); file.close(); QByteArray patch_data1 = QByteArray::fromHex("8800010026020000"); int64_t index = data.indexOf(patch_data1); int64_t index2 = data.indexOf(patch_data1,index+patch_data1.size()); qInfo() <<data.mid(index,17).toHex()<<index<< data.mid(index2,17).toHex() << index2;