How to best convert QString with comma to 4byteQstring hex

  • Hello I want to convert display text to QString 4 byte hex for example:

    QString input ->"119.1"
    float ->119.1
    int -> 1191
    hex int -> 4A7
    QString -> "4A7"
    QString output -> "04A7" always 4 byte.

    bool ok;
        int dec0 = (input.toFloat(&ok))*10;
        //QString dec1 = QString::number(dec0);
        QString c = QString::number(dec0, 16);
        if(c.size()<4) c = QString(4-c.size(), '0')+c;

    but when I put 29.3 output = 0124 not 0125.
    @edit with .toDouble work great but it's good way?

  • Moderators

    Casting to int is always rounding down. If you want to round to nearest integer use qRound or add 0.5 before casting.

    QString in = "119.1";
    QString out  = QString::number(qRound(in.toFloat() * 10.0), 16).toUpper().rightJustified(4, '0');

  • thank's work's perfectly!! :D

Log in to reply

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