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