Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

int QByteArray array



  • Dear all,

    So far i have used this:

    QByteArray writeData_green;
    writeData_green.resize(5);
    writeData_green[0] = 0x05;
    …
    

    but now i need to convert int which will be provided as variable, so lets say:

    int green = 150;
    QByteArray writeData_green;
    writeData_green.resize(5);
    writeData_green[0] = green.toHex();
    …
    

    and I need to write it with "0x" prefix and the HEX value to be in uppercases, so for example 0x8A

    how to do this?



  • @shokarta
    Use something like:

    writeData_green.setNum(green, 16);
    qDebug() << writeData_green.toHex();
    

    You will have to call QByteArray::toUpper() to get the uppercase letters, and you will have to insert your own 0x prefix, to get a final result of 0x8A. See https://stackoverflow.com/a/36603575/489865 for an example.



  • @shokarta
    Use something like:

    writeData_green.setNum(green, 16);
    qDebug() << writeData_green.toHex();
    

    You will have to call QByteArray::toUpper() to get the uppercase letters, and you will have to insert your own 0x prefix, to get a final result of 0x8A. See https://stackoverflow.com/a/36603575/489865 for an example.


Log in to reply