PDA

View Full Version : Can somebody show how to read bytes?



"BumbleBee"
19th April 2011, 14:57
Hey people,I've been searching for hours now about how to read bytes from a file.
What I want to do,is open an .exe file,take the bytes and output them as binary(100101)/hex(FFA2)...

I have searched this in plain c++ and Qt as well..but none good answer.
All I get are some strange chars,or a string "MZP" in console....

I have tried QFile,QIODevice,QByteArray..but no result.

Can anyone please explain me how to do this?

Thanks.;)

high_flyer
19th April 2011, 15:04
I have tried QFile,QIODevice,QByteArray..but no result.
Show what you have tried.

wysota
19th April 2011, 15:09
Hey people,I've been searching for hours now about how to read bytes from a file.
QIODevice::read()



All I get are some strange chars,or a string "MZP" in console....
And what did you expect to receive? A happy smiling face?

"BumbleBee"
19th April 2011, 15:35
Well,I have tried many different combination with different syntax...I cannot post a certain code,as there were many things I tried..

Can you please post a working piece?

high_flyer
19th April 2011, 15:36
Post the the code you thought was the best of the things you tried, and we can pick it up from there.

"BumbleBee"
19th April 2011, 15:44
Sure.
Well I made this:
#include <QtCore/QCoreApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QFile f("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe");
if( !f.exists() )

{
// It does not exist
qDebug() << "The file does not exist.\n";
}

// It exists, open it
if(!f.open(QIODevice::ReadOnly))
{
// It could not open
qDebug() << "Failed to open.";

return 0;
}

// It opened, now we need to close it
qDebug() << "Success";

int size = f.size();
qDebug() << "size: " << size;

QDataStream data(&f);
char * buffer;
uint u = sizeof(f);
data.readBytes(buffer,u);


f.close();
return a.exec();
}


Then I thought that I may need to make a loop,in order to read a byte after byte..but still nothing.

wysota
19th April 2011, 15:49
The code doesn't make much sense really. Why don't you just use QIODevice::read() on your QFile object?

mcosta
19th April 2011, 16:03
Sorry,



int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QFile f("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe");

....
int size = f.size();
qDebug() << "size: " << size;

QDataStream data(&f);
char * buffer;
uint u = sizeof(f);
data.readBytes(buffer,u);


f.close();
return a.exec();
}



what you're doing??


You're using a pointer "buffer" as array of bytes (WRONG)
You're using sizeof of an object as amount of data to read (WRONG)


for 1. it's strange that your program doesn't crash

I suggest you



int size = f.size();
qDebug() << "size: " << size;

QByteArray buffer = f.read (size);


and use directly "buffer"

squidge
19th April 2011, 17:21
All I get are some strange chars,or a string "MZP" in console....Which is the ascii representation of the file [All EXE files start with the letters 'MZ'], so you have already managed to read the file. All you need to do is convert your ascii into hex or binary. See the docs on how to do that or write your own conversion function.

ChrisW67
20th April 2011, 00:48
You do not need or want to use QDataStream for this task. As Wysota points out a simple call to QIODevice::read() or readAll() will suffice. As mcosta points out you are flirting with crashing by using a char* pointer that is not initialised to point at allocated memory: Qt provides structures to handle this for you.

Your problem comes down to:

Open file and report error if file not opened: See QFile and QIODevice open() and errorString().
Read file content into memory. See QByteArray and QIODevice::read() or readAll().
Close file. See QFile::close()
Do something with content. See QByteArray::toHex()

That description is about the same length as a basic implementation.

wysota
20th April 2011, 07:29
To suplement what was already said - I strongely discourage you from reading the whole file into memory since 1) you don't know how big it is and 2) most likely you don't need to have all the data available at once. Read as much as you need, process it and then read more if you need more.

"BumbleBee"
20th April 2011, 08:20
int size = f.size();

QByteArray bytes = f.readAll();

qDebug() << bytes; //outputs ""MZP

qDebug() << bytes.toHex(); //Same output :(

mcosta
20th April 2011, 08:40
What do you expect to read??

You're reading a binary file!

"BumbleBee"
20th April 2011, 08:41
What do you mean?

I want to convert that to hex...

mcosta
20th April 2011, 08:55
For me this code



char data [] = { 0xaa, 0x01, 0x00, 0xFF, 0xBA};
QByteArray ba (data, 5);

qDebug() << ba;
qDebug() << ba.toHex();


print



"ª
"aa0100ffba"

"BumbleBee"
20th April 2011, 09:00
How is this related to my source?
i get MZP when reading Anscii like wysota said.
But Chris said I have to convert that QBA to hex..which doesn't work...

wysota
20th April 2011, 09:15
But Chris said I have to convert that QBA to hex..which doesn't work...
What doesn't work? What's the point of dumping the whole file to the screen in its hexadecimal form?


#include <QFile>
#include <stdio.h>

int main(int argc, char **argv) {
QFile f(argv[1]);
f.open(QFile::ReadOnly);
int offset = 0;
while(!f.atEnd()){
QByteArray ba = f.read(16).toHex();
QString offsetStr = QString::number(offset, 16);
while(offsetStr.size()<8) offsetStr.prepend('0');
printf("0x%s\t", qPrintable(offsetStr));
offset+=16;

for(int i=ba.size()-2;i>0;i-=2) {
ba.insert(i, ' ');
}

printf("%s\n", ba.constData());
}
return 0;
}

mcosta
20th April 2011, 09:33
Why doesn't work?

This code



void Widget::on_openFileButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName (
this,
tr ("Open File"),
".",
tr ("Executable files (*exe)"));

if (!fileName.isEmpty ()) {
QFile f (fileName);

const int SIZE = 100;
int readSize;

f.open (QIODevice::ReadOnly);
readSize = (f.size () > SIZE)? SIZE: f.size ();

QByteArray ba = f.read (readSize);

ui->rawDataEdit->setPlainText (ba);
ui->hexDataEdit->setPlainText (ba.toHex ());
}
}


shows this output when I select a EXE file

6259

ChrisW67
20th April 2011, 09:43
int size = f.size();

QByteArray bytes = f.readAll();

qDebug() << bytes; //outputs ""MZP

qDebug() << bytes.toHex(); //Same output :(
Unpossible!

"BumbleBee"
20th April 2011, 10:25
What doesn't work? What's the point of dumping the whole file to the screen in its hexadecimal form?


#include <QFile>
#include <stdio.h>

int main(int argc, char **argv) {
QFile f(argv[1]);
f.open(QFile::ReadOnly);
int offset = 0;
while(!f.atEnd()){
QByteArray ba = f.read(16).toHex();
QString offsetStr = QString::number(offset, 16);
while(offsetStr.size()<8) offsetStr.prepend('0');
printf("0x%s\t", qPrintable(offsetStr));
offset+=16;

for(int i=ba.size()-2;i>0;i-=2) {
ba.insert(i, ' ');
}

printf("%s\n", ba.constData());
}
return 0;
}

I need to take the hex,in order to encrypt it and save it as a new exe...btw,your code makes an infinite loop....

FelixB
20th April 2011, 10:41
.btw,your code makes an infinite loop....

where is that infinite loop? I don't see any...

"BumbleBee"
20th April 2011, 10:46
#include "StdAfx.h"
#include <iostream>
#include <string.h>
using namespace std;

void encrypt_data();

int main()
{
FILE * f;
unsigned char buffer[100];
int n;

f = fopen("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe", "rb");
if (f)
{
n = fread(buffer, 100, 1, f);
for (int c = 0; c <= 100 ; c++)
{
printf("%.2X ", (int)buffer[c]); //outputs hex!!!!!!
// put an extra space between every 4 bytes
if (c % 4 == 3)
{
printf(" ");
}

// Display 16 bytes per line
if (c % 16 == 15)
{
printf("\n");
}
}
// Add an extra line feed for good measure
printf("\n\n\n");
}
else
{
cout << "error";
}

char a;
cin >> a;
return 0;
}

I want to make that in Qt^^^

And FelixB the loop doesn't stop for me.

wysota
20th April 2011, 10:53
btw,your code makes an infinite loop....
There is no infinite loop in my code.

FelixB
20th April 2011, 10:57
And FelixB the loop doesn't stop for me.

which loop do you mean?

"BumbleBee"
20th April 2011, 11:01
it keeps outputting bytes....never stops.

wysota
20th April 2011, 11:06
it keeps outputting bytes....never stops.

Maybe try pointing the program to a smaller file?

"BumbleBee"
20th April 2011, 11:11
Accually yes.It works with a smaller one.
Thank you for this,could you comment a bit the lines?
Thanks.

wysota
20th April 2011, 11:14
Thank you for this,could you comment a bit the lines?
No, I couldn't. It's so trivial there is nothing to comment. If you have specific questions then ask and I'm sure somebody will provide an explanation.

squidge
20th April 2011, 11:28
I need to take the hex,in order to encrypt it and save it as a new exe...btw,your code makes an infinite loop....Why do you want to encrypt the hex (text) version of a binary file? Why not just encrypt the binary version? Converting to text first makes no sense...

wysota
20th April 2011, 11:35
Maybe he wants to encrypt twice as much as he needs to? :rolleyes: It's always fun to make a 10MB file out of a 5MB file...

"BumbleBee"
20th April 2011, 13:09
Why do you want to encrypt the hex (text) version of a binary file? Why not just encrypt the binary version? Converting to text first makes no sense...

You mean like this?

QByteArray byte = file.read(16);

squidge
20th April 2011, 13:37
You mean like this?

QByteArray byte = file.read(16);

Yes, exactlly.

"BumbleBee"
20th April 2011, 13:58
This outputs some strange symbols(I suppose bin. code),but it also beeps and crashes after some secs...

wysota
20th April 2011, 16:53
Do you want to encrypt it or print it? I doubt encrypting it outputs any strange symbols or makes any sounds. This is binary data that is not suitable for printing to a text terminal.

"BumbleBee"
20th April 2011, 17:24
Well,crypt it of course,but I want to out put it,to make sure,everything works...and that I will encrypt the data I need.!

squidge
20th April 2011, 17:55
So encrypt the binary data but output it to a terminal as hex for the before and after.

"BumbleBee"
20th April 2011, 17:57
Yup.
Thanks guys for the support,help and sorry for the dump question...:o