PDA

View Full Version : redaing binary file



anh5kor
4th March 2016, 12:22
I have a requirement where I have to ready a binary file and search for a particular address given by user and store the hex value of corresponding address in an array.
I have a file like "39059921.HA". Which i have to read on a button click.
I have opened the file in hex viewer and the inside content are like below


Block 0 Strarts at:0x 0 Ends at: 0x4319 (Length:0x431A=17178)
00000000: 03 01 00 02 04 00 55 50 41 00 00 00 00 00 00 00 ......UPA.....
00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .................
00000020: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF .................
00000030: 66 65 74 79 00 00 00 00 00 00 00 00 CD 38 1A D1 .fety........
-------
-------


I tried reading the file like normal text file but i am getting some junk value.


QFile file("39059921.HA");

if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QTextStream stream(&file);
while(!stream.atEnd())
{
QString line = stream.readLine();
qDebug()<<"data"<<line;
}

file.close();

output:-I get the output like below
data""—¶GÐEÐk|‘s‰žÃ* òm[ˆ8êܳ$7¢,HYNܾlFTÛ/õêÃâlÂ¥Bóîä1©»0 *¢Ïƒ©è~Êßl«Ê2<ˆWXˆ³8{Èà IÃ…@"
data"&ÓûcVp„êp1¡Çø–Ú ñ T77”(/•´„ŸÎµ°©¯⠝9·|ÞmJ"5²ŒBÂ¡ó” ˜NWeB(¯e£÷•ˆ^à $Òo›ùt{€"“Ts²–)‡hexText "ppÀhexText "pqÆ pˆà p•ÂÐx‡õphà "

How can I read the above and search for a particular address

anda_skoa
4th March 2016, 13:53
As you said, it is a binary file so don't treat it like a text file.

Open it without the text flag, seek to the position, read as many bytes as you want using QIODevice::read() (which QFile is a subclass of).

Cheers,
_

anh5kor
8th March 2016, 09:52
Actually its a Hex file which contain address and 16 bit hex data like below


00000020: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

I have tried reading as below


QFile file("abcdef.dat");

if(!file.open(QIODevice::ReadOnly))
{
return;
}
QTextStream stream(&file);
while(!stream.atEnd())
{
QString line = stream.readLine();
QString hexText = QByteArray::fromHex(file.readLine());
qDebug()<<"Text"<<line;
qDebug()<<"hexText"<<hexText ;
}

file.close();

but still not able to read.
Please let me know can we parse or read the hex file as like other text file.

anda_skoa
8th March 2016, 10:34
Are you sure it is a text file that contains text that is hex codes?
Can a text editor open it?

Cheers,
_

anh5kor
8th March 2016, 11:29
The file I can open only with Hex view Tool.
It can not be open with normal notepad or notepad++. If you try it will show some junk character
Its not a text file.

anda_skoa
8th March 2016, 11:58
Then why do you keep treating it like a text file?

If a text editor can't read it, why do you expect QTextStream to be able to?

Are you aware that not all byte sequences can be interpreted as text?

Cheers,
_

ChrisW67
8th March 2016, 20:52
There is a common inability to tell the difference between the human-readable representation of a set of bytes and the actual bytes at play here. This leads to the incorrect notion that the hexadecimal representation == the actual bytes. Once your brain is there it seems logical that fromHex() would be needed.

As you have already pointed out, this is what the OP wants to read a few bytes from the middle of the file:


QFile file("abcdef.dat");
if(file.open(QIODevice::ReadOnly)) {
if (file.seek(100)) {
QByteArray bytes = file.read(6);
// do something with bytes
}
file.close();
}