Results 1 to 7 of 7

Thread: Save binary data into XML file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save binary data into XML file

    Quote Originally Posted by squidge View Post
    You can read the binary file into memory, encode as Base64 (using QByteArray::toBase64) and then place the data into a CDATA section of the XML using QDomDocument::createCDATASection, QXmlStreamWriter::writeCDATA, or even just QXmlStreamWriter::writeCharacters.
    I have similar issue.

    I have a pointer to a buffer contains Binary-data .. I want to pull the values inside the buffer and encoding them before putting them in my xml format .. so I though of this:

    QByteArray text;
    for (int i=0; i<len_of_my_buffer; i++){
    text[i] = *pointer_to_buffer[i];
    }
    create_xml_function(text.toBase64, "encoded data")

    is this right ?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Save binary data into XML file

    is this right ?
    No. A QByteArray is created empty with no size, so accessing text[ i ] on an empty array will cause a crash. Do this:

    Qt Code:
    1. QByteArray text64 = QByteArray::fromRawData( pointer_to_buffer, len_of_my_buffer ).toBase64();
    2. create_xml_function( text64, "encoded_data" ); // spaces not allowed in XML tags!
    To copy to clipboard, switch view to plain text mode 

    Of course, you will want to add some error checking...

  3. #3
    Join Date
    Mar 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save binary data into XML file

    Quote Originally Posted by d_stranz View Post
    No. A QByteArray is created empty with no size, so accessing text[ i ] on an empty array will cause a crash. Do this:

    Qt Code:
    1. QByteArray text64 = QByteArray::fromRawData( pointer_to_buffer, len_of_my_buffer ).toBase64();
    2. create_xml_function( text64, "encoded_data" ); // spaces not allowed in XML tags!
    To copy to clipboard, switch view to plain text mode 

    Of course, you will want to add some error checking...
    Thanks, it works now and I am passing it to my xml. you were right, my Qbytearray was "empty with no size"

Similar Threads

  1. Replies: 12
    Last Post: 31st October 2010, 17:08
  2. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24
  3. can`t read a binary data!
    By blm in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 16:56
  4. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:14
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

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
  •  
Qt is a trademark of The Qt Company.