Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Complete conversion from hexadecimal to binary
Forum Updated to NodeBB v4.3 + New Features

Complete conversion from hexadecimal to binary

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    atilalms
    wrote on last edited by
    #1

    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);@

    1 Reply Last reply
    0
    • B Offline
      B Offline
      butterface
      wrote on last edited by
      #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].

      1 Reply Last reply
      0
      • A Offline
        A Offline
        atilalms
        wrote on last edited by
        #3

        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@

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved