PDA

View Full Version : reading hex data from QFile



Charvi
29th October 2012, 09:29
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 ?



/* Opening file with the data stream to be written to HH */
QString filePath = QCoreApplication::applicationDirPath();
QString input = filePath + QString("/packet.txt");
packetDataFile = new QFile(input);

if( !packetDataFile->exists() )
{
qDebug()<<input<<" does not exist!";
return;
}

if( packetDataFile->isOpen() )
packetDataFile->close();

if( !packetDataFile->open(QFile::ReadOnly) )
{
qDebug()<<"Could not open file in readonly mode";
qDebug()<<qPrintable(packetDataFile->errorString());
return;
}

datagram = packetDataFile->readAll();
qDebug()<<datagram;

udpSock.writeDatagram(datagram, QHostAddress("10.99.14.100"), 5001);


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. :(

wysota
29th October 2012, 23:00
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?


QFile f("yourfile");
f.open(QIODevice::ReadOnly);
for(int i=0;i<8;++i) qDebug() << (unsigned int)f.read(1).at(0);

Charvi
30th October 2012, 11:54
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.



udpRecv.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

qDebug()<<"size of rx byte array: "<<datagram.size();
QByteArray temporary;
temporary = datagram.toHex();
qDebug()<<temporary.size();


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.