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. [SOLVED]QByteArray values to hex formatted QString
Qt 6.11 is out! See what's new in the release blog

[SOLVED]QByteArray values to hex formatted QString

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 23.2k 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.
  • J Offline
    J Offline
    JustGreg
    wrote on last edited by
    #1

    Hi!

    I'd like to display QByteArray byte values in my plaintextedit in the following format:

    00 00 00 .... 00 (20 values per line)
    00 00 00 ... 00
    ..etc

    I'm using QString::number ( qbytearray[i], 16) but it casts to long/int/uint formats and byte numbers>0x7f show up in the form of ffffffxx (for example if my byte=0xC0 then the output will be ffffffc0). I'm new to Qt so before I dive into improvised variable manipulations I'd like to ask if there is an easier method provided by Qt libraries. Thank you

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Did you consider using QByteArray::toHex() for that? The result will not be formatted like you are showing, but that should be easy enough to do as a next step.

      edit
      Alternatively, you could consider a completely different approach. You could create a custom model operating on your byte array, and use a QTableView to display the values instead.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        messi
        wrote on last edited by
        #3

        Hi JustGreg
        it sounds to me that you are trying to develop a HexEditor.
        If it so, have you checked:
        http://qt-apps.org/content/show.php/QHexEdit?content=133189

        Anyway, as usual the doc or google is your friend.
        google for "qt from int to hex "
        Gives you the following result:
        http://qt-project.org/forums/viewthread/7194
        http://www.developer.nokia.com/Community/Wiki/Convert_hexadecimal_to_decimal_and_vice-versa_in_Qt

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dvdk
          wrote on last edited by
          #4

          I think this would result in the wanted output since you want to interpret it as an unsigned value :
          @
          QString::number ((uchar) qbytearray[i], 16)
          @

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JustGreg
            wrote on last edited by
            #5

            [quote]Did you consider using QByteArray::toHex() for that?[/quote]

            Andre, thank you for the tip, I came up with a solution using the ::toHex method you suggested.

            [quote]it sounds to me that you are trying to develop a HexEditor.[/quote]

            Messi, thank you for your help, I'll check your links later. It is not a hex editor but a simple app which displays the received bytes from the serial port.

            [quote]I think this would result in the wanted output since you want to interpret it as an unsigned value [/quote]

            Thank you dvdk.

            Edit: @ tags are for code sections, not for quoting previous posts; Andre

            1 Reply Last reply
            0
            • M Offline
              M Offline
              messi
              wrote on last edited by
              #6

              in that case I would check the source of hexdump.
              I know it is in C but it gives you an idea how to program that routine quite fast.

              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