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. Problem Displaying QByteArray Binary String as Hex String in qDebug
Forum Updated to NodeBB v4.3 + New Features

Problem Displaying QByteArray Binary String as Hex String in qDebug

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 21.9k 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.
  • R Offline
    R Offline
    ReaMix
    wrote on 27 Apr 2012, 01:38 last edited by
    #1

    I want to debug my QByteArray of binary values, but it is much easier to read the binary value as a Hex string.

    Here's a simplified example of the code:
    @bool ok;
    qint32 offset = 0; // For cases when inserting values at non-word split (i.e. bit 13)
    qint32 value = 65494; // Hex value is FFD6

    QByteArray binValue;
    binValue.insert(offset, QString::number(value, 2));

    // Display value as Hex string
    qDebug() << binValue.data(); // shows "1111111111010110"
    qDebug() << binValue.toHex().data(); // shows "3131313131313131313130313031..."
    qDebug() << QString(binValue.data()); // shows "1111111111010110"
    qDebug() << QString::number(QString(binValue.data()).toInt(&ok, 16)); // shows "0"
    qDebug() << QString::number(QString(binValue.data()).toInt(&ok, 16), 16); // shows "0"@

    How can I display the binary string as a Hex string, "FFD6"?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on 27 Apr 2012, 02:08 last edited by
      #2

      What you need is writing a function by yourself ;-)

      @
      QByteArray bin2Hex(const QByteArray& input)
      {
      //...
      return ...
      }
      @

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on 27 Apr 2012, 04:31 last edited by
        #3

        Oh dear, another person confused by the difference between a number and its representation as a string. Your binValue QByteArray contains a string of 16 characters (bytes) that read to a human as the binary representation of the number. Converting those 16 bytes to 32 hexadecimal characters (your line 10) is nothing like the same thing as displaying the original number in hex. Converting those 16 characters to a number by treating it as if it were a hexadecimal representation (your lines 12/13) will not give you the original number and, in general will overflow an int... ok == false and return is 0.

        If you want value converted to a hex string then just ask for it that way:
        @
        int value = 65494; // the original number (it is in binary in memory)
        qDebug() << QString::number(value, 16); // displayed as a hex string

        // Converting a hex string back to a number
        bool ok;
        int value = QString("FFD6").toInt(&ok ,16);
        qDebug() << value << ok;
        @

        1 Reply Last reply
        0

        1/3

        27 Apr 2012, 01:38

        • Login

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