QByteArray::indexof always returning 1 (Qt)

36 Views Asked by At

   
    QByteArray bItem;
    QByteArray alpha("0123456789ABCDEF");
    quint8 iItemHi=0, iItemLo=0, index;

    QTableWidgetItem *hexItem = new QTableWidgetItem;
    hexItem = ui->hexSectorDisplay->item(x,y);
    bItem = hexItem->data(Qt::EditRole).toByteArray();
    if(bItem.size() == 1) {
        if(index=alpha.indexOf(bItem.at(0), 0) != -1) {
            iItemLo = index;
        } else {
            iItemLo=0;
        }

I have the above code to copy each cell from a QTableWidget (hexSectorDisplay) and then convert it to a QByteArray (bItem - can be either one or two characters representing a hex byte), then look each character up in another QByteArray (alpha) and use the returned index value as the numeric value of the hex character. Anyway, the problem is, "alpha.indexOf(bItem.at(0), 0)" always returns 1 (not -1) no matter what character is looked-up.

Any ideas what I am doing wrong here?. Please excuse me if my code is messy, I am not a seasoned C++/Qt programmer.

I am using Qt v5.15.2 with MSVC2019

1

There are 1 best solutions below

0
atari8warez On

Ok, I found the problem. When I took at the expression index=alpha.indexOf(bItem.at(0), 0) out of the if statement, and rewrote the code as follows, it worked:

index=alpha.indexOf(bItem.at(0), 0);

if(index != -1) {
  ... 
} else {
  ...
}