PDA

View Full Version : Convert ByteArray to Plaintext



TEAmerc
10th December 2015, 16:34
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,

ars
10th December 2015, 18:38
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

TEAmerc
10th December 2015, 18:41
Perfect, for some reason I didn't think to look for conversion functions!

Thanks,
TEAmerc

ChrisW67
10th December 2015, 20:11
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,...