Results 1 to 3 of 3

Thread: reading hex data from QFile

  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.

  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: reading hex data from QFile

    Quote Originally Posted by Charvi View Post
    I have a file that contains hex bytes.
    No, you don't. There is no such thing as "hex bytes". You just have a file Possibly containing non-textual data (contrary to what the file extension suggests).

    But all i get when i print is ascii values of the hex data.
    qDebug() displays byte arrays containing non-ascii data as ascii representation of the binary data contained within. It wouldn't do you much good to get a series of non-printable characters in the console.

    How do I get the hex data ?
    "hex" is only one of possible representations of data. The data itself is always "hex" (even textual one), it can just be printed as "hex", "oct", "bin", "dec", "ascii" or possibly some other encoding.

    Any thoughts ?
    The code itself is ok. It reads contents of the file and dumps it as a udp datagram.

    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.
    What exactly do you have in the file? E.g. what does the following print?

    Qt Code:
    1. QFile f("yourfile");
    2. f.open(QIODevice::ReadOnly);
    3. for(int i=0;i<8;++i) qDebug() << (unsigned int)f.read(1).at(0);
    To copy to clipboard, switch view to plain text mode 
    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. #3
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: reading hex data from QFile

    Prob.

    bytearray.toHex().data() stores each nibble as one byte. Like for 0x1d it stores 1 and d in differetn bytes. i want them to be one hex val. please help.

    Qt Code:
    1. udpRecv.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    2.  
    3. qDebug()<<"size of rx byte array: "<<datagram.size();
    4. QByteArray temporary;
    5. temporary = datagram.toHex();
    6. qDebug()<<temporary.size();
    To copy to clipboard, switch view to plain text mode 

    This gives :
    size of rx byte array: 29
    58

    toHex() gives a double sized byte array

    Hey some of the posts went away form between. I solved the above prob. Wysota was right no text file has hex. so I did a c prog and used strtoul() func to get hex data. Next Now m reading from UDP sock in a bytestream now I want to process the byte stream byte by byte and want to compare these bytes to some fixed value. But the above prob.
    Last edited by Charvi; 30th October 2012 at 12:08.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.