c++qtqstringqvector

Casting Qvector<uint8_t> to QString


I want to convert my uint8_t vector which corresponds to the hex values of the characters to the string. But I can not convert my vector to the string. Help please My code:

#include <QVector>
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QVector<uint8_t> vec;
    vec.push_back(51);
    vec.push_back(32);
    vec.push_back(123);
    QString message = "";
    qDebug()<<vec.at(0);

    for(int counter=0;counter<vec.length();counter++){
        message=vec.at(counter)+ message;
    }

    qDebug()<<message;
    //w.show();
    return a.exec();
}


Solution

  • Your question still obscure to me but as far as i understood you want qDebug() to output the numbers in hexadecimal.

    So it can be done via QByteAray. Possible implementation :

    #include <QVector>
    #include <QCoreApplication>
    #include <QDebug>
    #include <algorithm>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QVector<uint8_t> vec;
        vec.push_back(51);
        vec.push_back(32);
        vec.push_back(123);
    
        QByteArray data;
        std::copy( vec.begin() , vec.end() , std::back_inserter( data ) );
    
        qDebug() << data.toHex( ' ' );
    
        return a.exec();
    }
    

    Output is :

    33 20 7b

    Also it is possible to convert a single number to string in hexadecimal representation with QString::number( your_value , 16 ). 16 represents base in here.