Hi,
What I mean is how to read it byte by byte and do the conversion so that the output is a text file with numbers between 0 and 255.
Thanks
Hi,
What I mean is how to read it byte by byte and do the conversion so that the output is a text file with numbers between 0 and 255.
Thanks
RTFM - read this funny manual
To read data from file use QFile and to write this data to text file use QTextStream. And next time simple ask : how write data to text file.
timmu (26th November 2012)
I was wondering why the code below doesn't work. I'm trying to read a binary file byte by bite and convert it into text or numbers, preferably to numbers.
Qt Code:
char readChar; while(!in_stream.atEnd()) { in_stream >> readChar; if(in_stream.atEnd()) {break;} out_stream << readChar << "\n"; }To copy to clipboard, switch view to plain text mode
Then this should also allow reading binary file but how to convert it into int?:
Qt Code:
qint64 bufSize = 1024; char *buf = new char[bufSize]; qint64 dataSize; while (!in_stream.atEnd()) { dataSize = infile.read(buf, bufSize); }To copy to clipboard, switch view to plain text mode
Last edited by timmu; 26th November 2012 at 13:01.
Because You are using variable of type char. So if in readChar You have value 65 decimal in text file You will get letter A because 65 is ASCII code of letter A. In first example change line 14 to :This is abc of C/C++ language and have nothing to Qt.Qt Code:
out_stream << (int)readChar << "\n";To copy to clipboard, switch view to plain text mode
timmu (26th November 2012)
Thank you very much, this made it work. Now I have this problem:
The following script only outputs the values whose 'signed 8-bit value' is positive. If it is negative, the corresponding value is not written into the output file. Why is that?
Qt Code:
char readChar; while(!in_stream.atEnd()) { in_stream >> readChar; if(in_stream.atEnd()) {break;} out_stream << (int)readChar << "\n"; }To copy to clipboard, switch view to plain text mode
Try
Qt Code:
To copy to clipboard, switch view to plain text mode
Cheers,
_
timmu (27th November 2012)
If your file contains binary data then why are you reading it with QTextStream?
Hi Wysota,
How would you recommend to read the file? The code change recommended above didn't solve the problem, I'm still not seeing all of the binary values converted to numbers.
Thanks!
Last edited by timmu; 27th November 2012 at 08:57.
With QFile and its QIODevice API.
What I understand is that you simply want a hex dump of the file. The most trivial approach (albeit far from optimal in terms of memory) is:
Qt Code:
out.write(in.readAll().toHex());To copy to clipboard, switch view to plain text mode
You can do the same if you want a decimal encoding, only that you need to iterate over each byte in the array and convert it manually to a decimal value.
timmu (27th November 2012)
Thanks Wysota, this is a very good idea. Indeed, I actually need decimal encoding. If I need to iterate over each byte to make the conversion, then I cannot use readAll(), is that correct? In that case I need to look at each byte individually somehow.
I've been playing with the code below. I'd like to read all the bytes of the binary file "infile" one by one and output all the bits (in the form of 1/0) so that I get one number per line per line. Example:
1
0
1
1
0
1
0
0
What is the best way to achieve that?
Qt Code:
out.write(in.readAll());To copy to clipboard, switch view to plain text mode
Thanks.
I guess my question was how to do it byte by byte.
Qt Code:
while(!file.atEnd()) { char c; if(!file.readChar(&c)) break; print_bit_by_bit(c); }To copy to clipboard, switch view to plain text mode
timmu (11th December 2012)
Bookmarks