Results 1 to 4 of 4

Thread: Convert ByteArray to Plaintext

  1. #1
    Join Date
    Sep 2015
    Location
    Ontario, Canada
    Posts
    28
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Convert ByteArray to Plaintext

    I want to be able to save the state of my QMainWindow in my application with the QDockWidgets to an XML file, but the saveState() and saveGeometry() functions output data as a bytearray so when I try to view it in a text editor I just get garbage instead of the information as actual bytes.

    I wanted to know if there was a way I could get the state as a bytearray, convert it to be the text representation (based on say its ASCII representation here: http://www.asciitable.com/ ), store that in the XML, and when I wanted to restore the state, convert the extracted value from the XML back into a bytearray and pass that into the restoreState() function. This way XMLs can still be generated to save and load settings manually or with my application, and the data will be readable, though if the geometry and state elements are missing from the XML being manually created then I can just recreate the docks on load in a side by side configuration instead of in a particular layout.

    Thanks,

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Convert ByteArray to Plaintext

    Hello,

    for storing the byte array you could use QByteArray::toHex() to convert it to a hex representation. When loading, apply QByteArray::fromHex() get back the binary representation. With this approach your output file can be edited with any editor.

    Best regards
    ars

  3. The following user says thank you to ars for this useful post:

    TEAmerc (10th December 2015)

  4. #3
    Join Date
    Sep 2015
    Location
    Ontario, Canada
    Posts
    28
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Convert ByteArray to Plaintext

    Perfect, for some reason I didn't think to look for conversion functions!

    Thanks,
    TEAmerc

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Convert ByteArray to Plaintext

    when I try to view it in a text editor I just get garbage instead of the information as actual bytes.
    Just for the record, that "garbage" is the actual bytes. What you were expecting is some encoding of those bytes in a printable-characters-only format, i.e. Not the actual bytes. You could use hexadecimal as suggested. Another easy option is Base64. You could even convert each byte to a decimal string and string them together as a comma separated list; 1,78,47,128,34,167,...

Similar Threads

  1. qdoublevalidator and plaintext
    By daniel_muller in forum Newbie
    Replies: 1
    Last Post: 7th October 2013, 07:12
  2. conversion from bytearray to int produce zero
    By daemonna in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2010, 23:52
  3. plainText object and greek
    By funkybomber in forum Newbie
    Replies: 4
    Last Post: 4th May 2010, 07:43
  4. Read ByteArray
    By kewlcode in forum Qt Programming
    Replies: 19
    Last Post: 5th April 2009, 10:41
  5. Extract method on plaintext with QRegExp
    By jlbrd in forum Qt Programming
    Replies: 1
    Last Post: 2nd July 2006, 13:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.