PDA

View Full Version : Binary files and conversion



timmu
23rd November 2012, 12:11
I'd like to read binary files and generate an output consisting of normal 8-bit numerical values corresponding to the bytes.
In other words, the input is a binary fine and output should be an array of values 0-255.

1. How should the file be opened/read?
2. Is there an automatic conversion from binary to 8-bit?

Thank you!

Lesiok
23rd November 2012, 12:25
What do you think is the difference between binary data and bytes with values ​​from 0-255 ?

timmu
23rd November 2012, 12:34
Hi Lesiok,

There is no difference, the data are always stored as binary. In the text format there are things like newline character etc. My question was:
What would be the best way to get this conversion done and should that involve opening and reading the files in another way (such as using different file handling commands and classes) specific to Qt.

Thanks.

Lesiok
23rd November 2012, 14:21
What conversion ? You say : there is no difference. Can tell precisely what is binary file. Are this data formatted or this is a stream of bytes.
P.S.
You are asking about QDataStream (http://qt-project.org/doc/qt-4.8/qdatastream.html) or QFile (http://qt-project.org/doc/qt-4.8/qfile.html)

timmu
23rd November 2012, 15:08
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

Lesiok
23rd November 2012, 15:28
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.

anda_skoa
23rd November 2012, 16:06
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.


Just read from the input device using the QIODevice read API and use QString::number() on each byte, or read fixed sized blocks and create strings for each block by using QString::sprintf with as many arguments as you have bytes in your blocks.

Cheers,
_

timmu
26th November 2012, 13:43
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.



char readChar;

QFile infile(Inname); if(!infile.open(QIODevice::ReadOnly)) {exit(1);}
QTextStream in_stream(&infile);

QFile outfile(Outname); if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {exit(1);}
QTextStream out_stream(&outfile);


while(!in_stream.atEnd())
{
in_stream >> readChar;
if(in_stream.atEnd()) {break;}
out_stream << readChar << "\n";
}


Then this should also allow reading binary file but how to convert it into int?:



qint64 bufSize = 1024;
char *buf = new char[bufSize];
qint64 dataSize;
while (!in_stream.atEnd())
{
dataSize = infile.read(buf, bufSize);
}

Lesiok
26th November 2012, 14:29
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 :
out_stream << (int)readChar << "\n";
This is abc of C/C++ language and have nothing to Qt.

timmu
26th November 2012, 15:33
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?



char readChar;

QFile infile(Inname); if(!infile.open(QIODevice::ReadOnly)) {exit(1);}
QTextStream in_stream(&infile);

QFile outfile(Outname); if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {exit(1);}
QTextStream out_stream(&outfile);


while(!in_stream.atEnd())
{
in_stream >> readChar;
if(in_stream.atEnd()) {break;}
out_stream << (int)readChar << "\n";
}

anda_skoa
26th November 2012, 20:14
Try


out_stream << QString::number(static_cast<uchar>(readChar))


Cheers,
_

wysota
26th November 2012, 21:12
If your file contains binary data then why are you reading it with QTextStream?

timmu
27th November 2012, 09:49
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!

wysota
27th November 2012, 10:05
How would you recommend to read the file?
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:


QFile in("infile");
QFile out("outfile");
in.open(QIODevice::ReadOnly);
out.open(QIODevice::WriteOnly);
out.write(in.readAll().toHex());

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, 10:28
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.

wysota
27th November 2012, 10:40
If I need to iterate over each byte to make the conversion, then I cannot use readAll(), is that correct?
You can use readAll(). You can't use toHex().

timmu
10th December 2012, 14:20
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?



QFile in("infile");
QFile out("outfile");
in.open(QIODevice::ReadOnly);
out.open(QIODevice::WriteOnly);
out.write(in.readAll());

wysota
10th December 2012, 14:31
What is the best way to achieve that?
The same as earlier. Read data and convert it to the notation of your choice. One byte is eight bits. For each byte extract each subsequent bit and print it.

timmu
10th December 2012, 15:29
Thanks.

I guess my question was how to do it byte by byte.

wysota
10th December 2012, 15:45
I guess my question was how to do it byte by byte.


while(!file.atEnd()) {
char c;
if(!file.readChar(&c)) break;
print_bit_by_bit(c);
}

timmu
11th December 2012, 09:44
Thanks Wysota!

This following gives me all byte integer values but how do I get the individual bits out of it.
What I need is a continuous array of 0's and 1's.




void binarytobits()
{
int i, mybit;

QFile in("indata.dat");

if (!in.open(QIODevice::ReadOnly)) return;


QByteArray blob = in.readAll();
int size = blob.size();


for(i=0; i<size; i++)
{
mybit=blob.at(i); cout << mybit << "\n";
}

}


I wonder if this is a valid and good way to convert QByteArray to QBitArray



QByteArray blob = in.readAll();
QBitArray blob2=QVariant(blob2).toBitArray();

wysota
11th December 2012, 10:55
This following gives me all byte integer values but how do I get the individual bits out of it.
Do you have ANY C/C++ knowledge? So far we are guiding you step by step and you don't seem to be making any effort yourself...

Use bit shifting or masking to extract specific bits from a byte value. This is a strictly C/C++ task, please try to solve it yourself.

timmu
11th December 2012, 11:17
Thanks!

Yes I do, I just don't know the bit operations. I was curious if Qt had something to that effect.

wysota
11th December 2012, 11:32
There is no need for Qt to have "something to that effect" if the programming language already provides it. There is no QAddTwoIntegers class in Qt the same way as there is no QInteger and no QIntegerAsBitString class. Even if there was no bit shifting in C, you should still be able to think your way through using regular integer divison.

timmu
11th December 2012, 11:54
I was wondering why the code below cannot read binary file and create a file with two columns:
showing the numerical value of each byte and then also its binary representation?



void binarytobits()
{
int i, size;
char mybyte;

QFile in("infile.dat"); if (!in.open(QIODevice::ReadOnly)) return;
QFile out("outfile.txt"); if (!out.open(QIODevice::WriteOnly)) return;
QTextStream out_stream(&out);

QByteArray blob = in.readAll();
size = blob.size();

for(i=0; i<size; i++)
{
mybyte=blob.at(i);
QString myBits( QString::number( mybyte, sizeof(&mybyte) ) );
out_stream << mybyte << " " << myBits <<"\n";
}

}

wysota
11th December 2012, 11:57
Read the docs for QString::number().

Hint: sizeof(&mybyte) = sizeof(char*) = 4 (or 8)

timmu
11th December 2012, 12:15
Indeed sizeof(char*) is 4 in my case.

Are you suggesting that QString::number() is not appropriate for what I'm doing?

The code above is generating a binary file and I cannot open it with text editor. Why is it binary?

wysota
11th December 2012, 12:19
Are you suggesting that QString::number() is not appropriate for what I'm doing?
I'm suggesting you should read the documentation.


The code above is generating a binary file and I cannot open it with text editor. Why is it binary?
No idea. Text files are also binary files.