PDA

View Full Version : Reading value from hex address in QT



JohnnyUSA
13th October 2016, 12:58
Hello guys, newbie QT programmer here stuck and in need of help :)

Case scenario is as follows: I have a binary file that i've managed to write data at specific address using the following piece of code:


QFile file ("example.dat";
QString data = "30";

file.open(QIODevice::ReadWrite);
QByteArray fileData(settings.readAll());

QByteArray data_write = QByteArray::fromHex(data.toLatin1());
fileData.replace(0x00000008,1,data_write);

file.seek(0);
file.write(fileData);
file.flush();
file.close();


This piece of code works perfectly by writing 30 to 0x00000008 address.

Now, what i fail to do, is reading the data back from 0x00000008 address for further manipulation. So, how can i manage to read the data from that specific binary file address? I'm searching on the web for 4 days already and still no solution. Hope you guys can help me out.

Thanks in advance!

Lesiok
13th October 2016, 13:02
QIODevice::seek and QIODevice::read

JohnnyUSA
13th October 2016, 13:46
QIODevice::seek and QIODevice::read

Thanks, gonna read about those and try to implement them. If i fail, im gonna ask your help again ;)

Added after 41 minutes:

Thanks again mate and sorry for double posting, using your advice i finally found the solution!



QFile file ("example.dat";
file.open(QIODevice::ReadOnly);
file.seek(0x00000018);

QByteArray bytes = settings.read(1);
file.flush();
file.close();

qDebug() << bytes.toHex();


Thanks again! A beer on you.

JohnnyUSA
14th October 2016, 13:41
Backto you guys with another annoying problem that i can't solve. Topic is the same so i didnt feel the need to create a new one.

Case scenarios is as follows. After i read the data as solved in yesterday's post, i need to use endianess to show the correct value on my UI. I do this using:


const int horizontal = qFromLittleEndian<qint16>(reswidth.data())


Now, using my ui controls, i change the value and i need to write it back to my binary file using this piece of code:


int width_int = width->value();
QString width_string = QString::number(width_int, 16);

QFile settings("example.dat");

settings.open(QIODevice::ReadWrite);
QByteArray fileData(settings.readAll());

QByteArray hex1 = QByteArray::fromHex(width_string.toLatin1());
fileData.replace(0x0000000C,2, hex1);
settings.seek(0);
settings.write(fileData);
settings.flush();
settings.close();

qDebug() << hex1;


On my binary file, it writes "02 80" but i want "80 02". Same for qDebug output ""\x02\x80" but i want to swap bytes on writing and the ouput to be "\x80\x02".

I know it's all about endianess but i don't know how to properly implement it into my code..


*Note width->value(); is a value from QSpinBox.

Thanks in advance!

Lesiok
14th October 2016, 13:54
Strange, you know the function of qFromLittleEndian and you do not know qToLittleEndian ?

JohnnyUSA
14th October 2016, 13:59
Strange, you know the function of qFromLittleEndian and you do not know qToLittleEndian ?

i've tried:


qDebug() << qToLittleEndian(hex1);


and it outputs the same "\x02\x80".

Lesiok
14th October 2016, 14:36
And what is the output from :
QByteArray tab;
tab.resize(4);
qToLittleEndian(width_int,tab.data());
qDebug() << tab;

JohnnyUSA
14th October 2016, 14:40
And what is the output from :
QByteArray tab;
tab.resize(4);
qToLittleEndian(width_int,tab.data());
qDebug() << tab;

It's ""\x80\x02\x00\x00""

Cheers, mate, i'm working on this example you gave me!

Lesiok
14th October 2016, 16:09
One more thing : You don't need to read all file to memory, modify it and write. Just open file, seek and write to them.

JohnnyUSA
14th October 2016, 23:09
One more thing : You don't need to read all file to memory, modify it and write. Just open file, seek and write to them.

Correct, thanks for the hint mate ;)