Converting 4 Byte array into float using little endianness
-
@vicksoccer You should rather use http://doc.qt.io/qt-5/qdatastream.html to read/write binary data.
-
Thanks all for your kind support!!!
Actually I am reading file which includes AScii characters. I have read all data from file and stored into QString.QFile slcFile(fileName); // filename is path of file.
QTextStream in(&slcFile);
QString slcText = in.readAll();yes I understood I can store data as QBytearray also. After storing I have process string data, But while reading data slcText does not include "\n" after end of line in QFIle. If I had open original file with texteditor (notepad++) then I was able see location of end of line .. but for many end lines slcText does not include "\n" and I need resulted string with "\n" to process. It happens same with QBytearray. If I get reading all data with "\n" my problem will be solved. Any suggestions for this...
-
@vicksoccer said in Converting 4 Byte array into float using little endianness:
Actually I am reading file which includes AScii characters
Well, in your first post you wrote "I have to read binary data from file". This is confusing at least.
So, lets make it clear: you are reading a text file - is that correct?
You can read the file without QTextStream like shown here: http://doc.qt.io/qt-5/qfile.html -
@vicksoccer If your file is mixed text/binary, you should read it as binary and process the data yourself.
Once it is converted to text only (QString), important information will be lost. So read as binary, and convert only the chunks that are known as text to QString.
Regards
-
@vicksoccer Check the link I posted...
-
@vicksoccer If the file really contains \n then you will get them when reading. How do you know it does not read them?
Also if you want read line by line use readLine(). -
@jsulm I have opened original file with texteditor(notepad++) there I can see data line by line..
But after reading file with above qt syntax the resulted QString or Qbytearray does not contain "\n" character at end of the line.
I have tried with readline() also but I am getting truncated string at wrong location ideally it comes at end line..
here my data is (opened in notpad++)
this is what I am getting in QString after readall()
here I am expecting "\n" character after index of 540.. -
@vicksoccer Can you show exact code you use to read the file?
-
@jsulm here my code is for reading file
QFile slcFile(fileName); //path of the file
if(!slcFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
showStatus("Unable to open the file\n"); //MessageBOx
return;
}
QTextStream in(&slcFile);
QString slcText = in.readAll(); //Reading data as string -
@vicksoccer And do you have an example for such a file?
Also, wasn't you question about binary float representation? How does that fit with a text file?
-
@vicksoccer said in Converting 4 Byte array into float using little endianness:
here I am expecting "\n" character after index of 540
Why? From what I could see from your screenshots this is a binary file. The fact that you can see the text in it in your notepad++ doesn't make it a text file. If you don't trust me open an executable in notepad++ and you're going to see the static strings from your program (in one way or another).
-
@aha_1980 Previously I was stucked at conversion of 4 QBytearray into float thats why I am posted with this..I am truely new in
this but now I am getting to know that my conversion is messed up due to skiping "\n" character at end line in receiving data as QString.
here is sample .slc file
https://www.dropbox.com/s/5bez93mdjr64v8x/open_display_SLC.zip -
@vicksoccer By looking at your
sample_ring2.slc
, I doubt that is a text file. It looks like pure binary data for me. -
@aha_1980 I have tried to read this file with
@
QFile slcFile(fileName); //path of the file
if(!slcFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
showStatus("Unable to open the file\n"); //MessageBOx
return;
}
QByteArray slcText = slcFile.readAll();
@
But still missing "\n" in QBytearray in required index.
I have take 4 data from QBytearray and convert into float value in loop so due missing "\n" index my conversion is messed up after performing some loop and does not getting expected float value. -
@vicksoccer said in Converting 4 Byte array into float using little endianness:
But still missing "\n" in QBytearray in required index.
There's no such requirement when working with binary files. This is relevant only for text files, which you don't have in this case.
-
@kshegunov Thanks for your reply..
Then how I can read this file can you just show me bunch of codes.. that will be nice -
@vicksoccer said in Converting 4 Byte array into float using little endianness:
can you just show me bunch of codes
I can't because each binary file has its own format and is different in that regard. You should read on the file format's specification to understand what is written how before proceeding with code.
-
@aha_1980 @Qt-Champions-2016 @Moderators
Please anyone can help me to read this sample_ring2.slc file I am trying to decode this slc file in qt