Results 1 to 5 of 5

Thread: Strange binary file output when using QByteArray.

  1. #1
    Join Date
    Dec 2014
    Posts
    4
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Strange binary file output when using QByteArray.

    Hi, all,

    I have a problem of QByteArry.
    When I take it to store a data, a strange binary file is outputted.
    A basic code is like as following.
    Qt Code:
    1. QString fname ="test1.db";
    2. QFile ext_file(fname);
    3. ext_file.open(QIODevice::WriteOnly);
    4. QDataStream out(&ext_file);
    5. static const char mydata[] = { 0x74,0x68,0x65 };
    6. QByteArray data = QByteArray::fromRawData(mydata,sizeof(mydata));
    7. out<<data;
    To copy to clipboard, switch view to plain text mode 

    However, I got a strange extra data on the head of data stream.

    Qt Code:
    1. 0000 0003 7468 65
    To copy to clipboard, switch view to plain text mode 

    Why does the output data have 0000 0003 ?
    Could you please help me.
    Thank you very much.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange binary file output when using QByteArray.

    QDataStream is a serialization mechanism and not a general purpose binary stream. It prepended the length of the byte array to the stream to be able to deserialize it later.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    schunyeh (25th December 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Strange binary file output when using QByteArray.

    Just to emphasis what wysota wrote: you problem has nothing to do with QByteArray.
    Write it directly into the file instead of serializing it as an object.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    schunyeh (25th December 2014)

  6. #4
    Join Date
    Dec 2014
    Posts
    4
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Strange binary file output when using QByteArray.

    Thank you, anda_skoa and wysota.
    I will try to use std::fstream or fwrite.

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange binary file output when using QByteArray.

    It is enough to use QFile::write.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. strange output using topercentencoding()
    By raj_iv in forum Qt Programming
    Replies: 3
    Last Post: 5th December 2012, 08:25
  2. Overlapping binary files with QByteArray stream
    By CodeHunt in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2012, 11:08
  3. Parsing/extracting from a binary QByteArray
    By Phlucious in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2011, 20:15
  4. Strange QByteArray mismatch
    By giusepped in forum Qt Programming
    Replies: 0
    Last Post: 25th October 2011, 18:28
  5. dynamicCall and QByteArray - strange characters
    By franco.amato in forum Qt Programming
    Replies: 120
    Last Post: 28th April 2010, 22:11

Tags for this Thread

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.