Why does one conversion fail but not the other?



  • Here is a sample of my logfile (with header):

    Date,Time(UTC),Bus Name,PGN#,PGN Priority,PGN Src. Addr.,PGN Dest. Addr.,Data Length,DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7
    20151029,140003,NMEA2000,127250,2,01,ff,8,ff,f1,bc,ff,7f,ff,7f,fd
    20151029,140040,NMEA2000,129025,2,03,ff,8,ab,23,8e,13,0e,8f,6b,b9

    I've built QStringLists with the data that I need (DB0, DB1...)

    And I'm trying to make it human readable. Here is my code to get at the GPS position in the log data:

    void MainWindow::on_btnPosition_clicked()
    {
    
        //BUILD LATITUDE HEX NUMBER FROM LOG DATA
        PGNListCount = PGNList.count();
        for (int i=0;i<PGNListCount;++i) {
            if (PGNList.at(i).contains("129025")) {
                DB30Data << DB3.at(i) + DB2.at(i) + DB1.at(i) + DB0.at(i);
                }
         }    
        qDebug() << "DB30data" << DB30Data.at(10);
    
        //BUILD LONGITUDE HEX NUMBER FROM LOG DATA
        PGNListCount = PGNList.count();
        qDebug() << "PGNListCount" << PGNListCount;
        for (int i=0;i<PGNListCount;++i) {
            if (PGNList.at(i).contains("129025")) {
                DB74Data << DB7.at(i) + DB6.at(i) + DB5.at(i) + DB4.at(i);
    
                }
         }
        qDebug() << "DB74Data" << DB74Data.at(10);
    
    
    
        //CONVERT LATITUDE HEX TO FLOAT
        int DB30DataCount = DB30Data.count();
        for (int i=0; i<DB30DataCount;++i) {
                latitudeFromHex = ((DB30Data.at(i).toInt(&ok, 16))*0.0000001);
                if (!ok){
                    qDebug() << "latitude conversion failed";
                    break;
                }
                Latitude.append(latitudeFromHex);
        }
        qDebug() << "Latitude" << QString::number(Latitude.at(10),'f',7);
    
    
        //CONVERT LONGITUDE HEX TO FLOAT
        int DB74DataCount = DB74Data.count();
        qDebug() << "DB74DataCount" << DB74DataCount;
        for (int i=0; i<DB74DataCount; ++i) {
                longitudeFromHex = ((DB74Data.at(i).toInt(&ok, 16))*0.0000001);
                if (!ok){
                    qDebug() << "longitude conversion failed";
                    break;
                }
                Longitude.append(longitudeFromHex);
        }
        qDebug() << "Longitude" << QString::number(Longitude.at(10),'f',10);
    
    }
    

    And here are the resulting debug statements:

    Debugging starts
    DB30data "138e23ab"
    PGNListCount 1000
    DB74Data "b96b8f1d"
    Latitude "32.8082347"
    DB74DataCount 200
    longitude conversion failed
    ASSERT failure in QVector<T>::at: "index out of range", file C:\Qt\5.5\mingw492_32\include/QtCore/qvector.h, line 393
    Debugging has finished

    Latitude and Longitude are declared as QVector<long double>
    latitudeFromHex and longitudeFromHex are declared as long double

    Why is the Latitude conversion working, but the Longitude conversion failing? This is a case where I need a nudge in the right direction - I haven't turned up anything in my own research.

    Thanks for any help!


  • Qt Champions 2016

    Have a look at the stack trace, it will point you to the exact line in your code where you overrun the vector.



  • I tried setting a breakpoint and stepping through while watching the stack, but it doesn't crash anywhere - it just drops to the boolean check and breaks out. If I take the check out, it just loads the array with zeros.



  • @MScottM You have

        if (!ok){
                    qDebug() << "longitude conversion failed";
                    break;
                }
                Longitude.append(longitudeFromHex);
        }
        qDebug() << "Longitude" << QString::number(Longitude.at(10),'f',10);
    

    What does it show if you change to

    qDebug() << "longitude conversion failed at " << i;
    

    and

    qDebug() << "Longitude count " << Longitude.count();
    qDebug() << "Longitude" << QString::number(Longitude.at(10),'f',10);
    


  • @Eeli-K Thanks for the idea!

    Debug statements:

    DB30data "138e23ab"
    PGNListCount 1000
    DB74Data "b96b8f0e"
    Latitude "32.8082347"
    DB74DataCount 200
    longitude conversion failed at 0
    longitude count 0
    ASSERT failure in QVector<T>::at: "index out of range", file C:\Qt\5.5\mingw492_32\include/QtCore/qvector.h, line 393
    Debugging has finished

    I put all three debug statements in the 'if' check like this:

    if (!ok){
                    qDebug() << "longitude conversion failed at " << i;
                    qDebug() << "longitude count" << Longitude.count();
                    qDebug() << "Longitude" << QString::number(Longitude.at(i),'f',10);
                    break;
                }
    


  • @MScottM It's obvious that

    longitudeFromHex = ((DB74Data.at(i).toInt(&ok, 16))*0.0000001);
    

    doesn't work as you expect. Break it down to pieces, one new function call/assignment per line, and follow it with debugger or add a qDebug line after each line.



  • Got it! I had to convert to LongLong:

    longitudeFromHex = ((DB74Data.at(i).toLongLong(&ok, 16))*0.0000001);

    did the trick.

    Thanks for taking the time to post!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.