[SOLVED]QByteArray values to hex formatted QString



  • 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



  • 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.



  • 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



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



  • [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



  • 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.


Log in to reply
 

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