PDA

View Full Version : How to decode string to human readable format



Mikhail86
11th May 2019, 08:34
I have a binary file. I need to read it and get a QString in human readable format.

I do like this.



QFile * file = new QFile("file1.bin");

if(file->open(QIODevice::ReadOnly | QIODevice::Text))
{
QByteArray ba = file->readAll();
qDebug() << ba;
}


qDebug gives me


"Z\x04\xFF?7\xC8\x10\x00\x00\x00\x00\x00?\x00\x00\x 00\x00\x00\x00\x00 9K86AT25Y6GV\x03\x00\x00@\x00\x00""00000081UFIJST UJM2A23B0 H2G \x10\x80\x00\x00\x00/\x00@\x00\x02\x00\x02\x07\x00\xFF?\x10\x00?\x00\x1 0\xFC\xFB\x00\x10\x01\xFF\xFF\xFF\x0F\x00\x00\x07\ x00\x03\x00x\x00x\x00x\x00x\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x1F\x00\x06\x17\x00\ x00L\x00@\x00\xF8\x01""B\x00k4\t\x7F""cai0\t\xBE""ca? \xA0\x00\x00\x00\x80@\xFE\xFF\x00\x00\xFE\xFE\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\xB0\xEA""B%\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x0 0P\xE0\x00\x88""D\xED\xD7\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x1D@\x1C@\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x8A\x00\x00\x00\x00\x00\x00\x04@\x00@\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00=\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x1F\x10\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x01\x00\x00\f\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\xA5""A"


What is this encoding and how can I translate it to readable QString

ChristianEhrlicher
11th May 2019, 09:20
How should we know what's the internal structure of your file?
What do you want to display? The hex values of the data? The take a look at QByteArray::toHex()

Mikhail86
11th May 2019, 09:37
The whole information is 255 words here. Each word is 2 bytes. The filesize is 511 bytes
I need to parse it and to find out information in words 27-46

anda_skoa
11th May 2019, 10:18
The whole information is 255 words here. Each word is 2 bytes.

A "word" in such a context is usually not a string but a number, a two byte integer.

For parsing such numbers you will need to know which of the two bytes has the higher value.
This can either be the first (also referred to as Big Endian) or the second (also referred to as Little Endian).

You then take each byte and put it in the correct position of a large enough integer type, usually by setting the higher byte first, shifting it by 8 bits and then adding the lower byte.

Cheers,
_

Mikhail86
11th May 2019, 11:15
A "word" in such a context is usually not a string but a number, a two byte integer.

For parsing such numbers you will need to know which of the two bytes has the higher value.
This can either be the first (also referred to as Big Endian) or the second (also referred to as Little Endian).

You then take each byte and put it in the correct position of a large enough integer type, usually by setting the higher byte first, shifting it by 8 bits and then adding the lower byte.

Cheers,
_

And what does Z at the beginning mean?

Also I can use QByteArray to read byte by byte anyway. But can you explain why the whole size is 511? Perhaps it should be 249?

Added after 39 minutes:

Also what does "bits 15:3" for example mean?
This binary file contains device identification information

ChristianEhrlicher
11th May 2019, 11:33
But can you explain why the whole size is 511? Perhaps it should be 249?


Again: we don't know what data you want to parse or how big it is.
Read it into a QByteArray and then you can access the single values with QByteArray::at()

anda_skoa
11th May 2019, 12:59
And what does Z at the beginning mean?


That obviously depends on the format specification.
It might not even be a "Z" character, the bits could be part of a larger value.

Cheers,
_

d_stranz
11th May 2019, 18:38
This binary file contains device identification information

So if that is the case, isn't there documentation for this device that describes how to interpret the binary information? Or something from the operating system that documents how information for such devices is written in their device description files?

The stuff you see when you ask QDebug to print the byte array is the best QDebug can do to translate each byte into a human-readable string. Some of the bytes map onto ASCII characters, like the "Z" at the start. Others cannot be mapped to a printable ASCII character, so QDebug writes them in a hexadecimal form: "\x00" is a byte with the value 0, "\xFF" is a byte with the value 255, for example.

Some of the information in your file that is shown as longer lengths of continuous ASCII characters are probably the name or id for the device, but without a description of what the binary file is supposed to contain, it is impossible to interpret it.

ChrisW67
12th May 2019, 12:08
I have a binary file.
OK.


I do like this.

QFile * file = new QFile("file1.bin");
if(file->open(QIODevice::ReadOnly | QIODevice::Text))
{
QByteArray ba = file->readAll();
qDebug() << ba;
}

You almost certainly do not want QFile to do line ending translations: depending on platform this may change the data. That is, you do not want QIODevice::Text in the options.