Qt World Summit: Register Today!

Complete conversion from hexadecimal to binary

  • Hello, everyone!

    I am reading bytes from serial port and I need to get one of these bytes and convert it from hexadecimal to binary. I got it, but I want to show the 8 bits from the byte, because I will work with them in the future.

    In the code below, I can show the bytes in binary, but I can't show the zeros in the left size to complete 8 bits. I need it because I want to extract each bit from this byte to work after that.

    @int flag_one = REGISTRO1.toInt(&ok,16); // Informações do elevado
    QString flag_converted = QString::number(flag_one,2);@

  • You can use QString::sprintf("% 08d", i) after converting your binary string to an integer. (remove the space between the % and 0.)

    Second possibility would be to generate a string filled with 0 and replace characters beginning from [7].

  • Thanks a lot, butterface! We solved the problem.
    I used the second possibility with QString::arg() and filled with '0':

    @QString::arg(const QString & a, int fieldWidth = 0, QChar fillChar = QLatin1Char( ' ' )) const@

Log in to reply