Results 1 to 3 of 3

Thread: reading hex data from QFile

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default reading hex data from QFile

    hi all,

    I have a file that contains hex bytes. I have to send these hex bytes over network using UDP. I opened the file read and sent in following manner. But all i get when i print is ascii values of the hex data. May be its because I am using the byte array to read data from file. How do I get the hex data ?

    Qt Code:
    1. /* Opening file with the data stream to be written to HH */
    2. QString filePath = QCoreApplication::applicationDirPath();
    3. QString input = filePath + QString("/packet.txt");
    4. packetDataFile = new QFile(input);
    5.  
    6. if( !packetDataFile->exists() )
    7. {
    8. qDebug()<<input<<" does not exist!";
    9. return;
    10. }
    11.  
    12. if( packetDataFile->isOpen() )
    13. packetDataFile->close();
    14.  
    15. if( !packetDataFile->open(QFile::ReadOnly) )
    16. {
    17. qDebug()<<"Could not open file in readonly mode";
    18. qDebug()<<qPrintable(packetDataFile->errorString());
    19. return;
    20. }
    21.  
    22. datagram = packetDataFile->readAll();
    23. qDebug()<<datagram;
    24.  
    25. udpSock.writeDatagram(datagram, QHostAddress("10.99.14.100"), 5001);
    To copy to clipboard, switch view to plain text mode 

    Any thoughts ?

    [EDIT]: okay made some changes in code. Now the datagram at qdebug() prints correct but at the receiver side still the data rx is ascii



    Okay did some digging. the docs at:<a href="http://qt-project.org/doc/qt-4.8/qfile.html#reading-files-directly">http://qt-project.org/doc/qt-4.8/qfile.html#reading-files-directly</a> say: The QIODevice::Text flag passed to open() tells Qt to convert Windows-style line terminators ("\r\n") into C++-style terminators ("\n"). By default, QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file. then why so in my case ? At rx side I print values in hex and receive ascii, for ex: b -> 62, for d -> 64, etc.
    Last edited by Charvi; 29th October 2012 at 10:18.

Similar Threads

  1. Replies: 2
    Last Post: 24th July 2011, 21:01
  2. Reading from QFile when QTextStream is open
    By dawwin in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2011, 17:43
  3. Problems reading content of QFile
    By martinn in forum Newbie
    Replies: 12
    Last Post: 6th April 2010, 18:42
  4. QFile, QDataStream reading binary data
    By yren in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2009, 06:34
  5. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2006, 23:12

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.