Page 1 of 2 12 LastLast
Results 1 to 20 of 37

Thread: Can somebody show how to read bytes?

  1. #1
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Can somebody show how to read bytes?

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    I have tried QFile,QIODevice,QByteArray..but no result.
    Show what you have tried.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by "BumbleBee" View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    Post the the code you thought was the best of the things you tried, and we can pick it up from there.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    Sure.
    Well I made this:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QFile>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. QFile f("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe");
    10. if( !f.exists() )
    11.  
    12. {
    13. // It does not exist
    14. qDebug() << "The file does not exist.\n";
    15. }
    16.  
    17. // It exists, open it
    18. if(!f.open(QIODevice::ReadOnly))
    19. {
    20. // It could not open
    21. qDebug() << "Failed to open.";
    22.  
    23. return 0;
    24. }
    25.  
    26. // It opened, now we need to close it
    27. qDebug() << "Success";
    28.  
    29. int size = f.size();
    30. qDebug() << "size: " << size;
    31.  
    32. QDataStream data(&f);
    33. char * buffer;
    34. uint u = sizeof(f);
    35. data.readBytes(buffer,u);
    36.  
    37.  
    38. f.close();
    39. return a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can somebody show how to read bytes?

    The code doesn't make much sense really. Why don't you just use QIODevice::read() on your QFile object?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    Sorry,

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QFile f("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe");
    6.  
    7. ....
    8. int size = f.size();
    9. qDebug() << "size: " << size;
    10.  
    11. QDataStream data(&f);
    12. char * buffer;
    13. uint u = sizeof(f);
    14. data.readBytes(buffer,u);
    15.  
    16.  
    17. f.close();
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    what you're doing??

    1. You're using a pointer "buffer" as array of bytes (WRONG)
    2. 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

    Qt Code:
    1. int size = f.size();
    2. qDebug() << "size: " << size;
    3.  
    4. QByteArray buffer = f.read (size);
    To copy to clipboard, switch view to plain text mode 

    and use directly "buffer"
    Last edited by mcosta; 19th April 2011 at 17:07. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  9. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by "BumbleBee" View Post
    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.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can somebody show how to read bytes?

    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:
    1. Open file and report error if file not opened: See QFile and QIODevice open() and errorString().
    2. Read file content into memory. See QByteArray and QIODevice::read() or readAll().
    3. Close file. See QFile::close()
    4. Do something with content. See QByteArray::toHex()

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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can somebody show how to read bytes?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    Qt Code:
    1. int size = f.size();
    2.  
    3. QByteArray bytes = f.readAll();
    4.  
    5. qDebug() << bytes; //outputs ""MZP
    6.  
    7. qDebug() << bytes.toHex(); //Same output :(
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    What do you expect to read??

    You're reading a binary file!
    A camel can go 14 days without drink,
    I can't!!!

  14. #14
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    What do you mean?

    I want to convert that to hex...

  15. #15
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    For me this code

    Qt Code:
    1. char data [] = { 0xaa, 0x01, 0x00, 0xFF, 0xBA};
    2. QByteArray ba (data, 5);
    3.  
    4. qDebug() << ba;
    5. qDebug() << ba.toHex();
    To copy to clipboard, switch view to plain text mode 

    print

    Qt Code:
    1. "ª
    2. "aa0100ffba"
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  16. #16
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    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...

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by "BumbleBee" View Post
    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?

    Qt Code:
    1. #include <QFile>
    2. #include <stdio.h>
    3.  
    4. int main(int argc, char **argv) {
    5. QFile f(argv[1]);
    6. f.open(QFile::ReadOnly);
    7. int offset = 0;
    8. while(!f.atEnd()){
    9. QByteArray ba = f.read(16).toHex();
    10. QString offsetStr = QString::number(offset, 16);
    11. while(offsetStr.size()<8) offsetStr.prepend('0');
    12. printf("0x%s\t", qPrintable(offsetStr));
    13. offset+=16;
    14.  
    15. for(int i=ba.size()-2;i>0;i-=2) {
    16. ba.insert(i, ' ');
    17. }
    18.  
    19. printf("%s\n", ba.constData());
    20. }
    21. return 0;
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 20th April 2011 at 10:23.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can somebody show how to read bytes?

    Why doesn't work?

    This code

    Qt Code:
    1. void Widget::on_openFileButton_clicked()
    2. {
    3. QString fileName = QFileDialog::getOpenFileName (
    4. this,
    5. tr ("Open File"),
    6. ".",
    7. tr ("Executable files (*exe)"));
    8.  
    9. if (!fileName.isEmpty ()) {
    10. QFile f (fileName);
    11.  
    12. const int SIZE = 100;
    13. int readSize;
    14.  
    15. f.open (QIODevice::ReadOnly);
    16. readSize = (f.size () > SIZE)? SIZE: f.size ();
    17.  
    18. QByteArray ba = f.read (readSize);
    19.  
    20. ui->rawDataEdit->setPlainText (ba);
    21. ui->hexDataEdit->setPlainText (ba.toHex ());
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    shows this output when I select a EXE file

    imagine1.JPG
    A camel can go 14 days without drink,
    I can't!!!

  19. #19
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by "BumbleBee" View Post
    Qt Code:
    1. int size = f.size();
    2.  
    3. QByteArray bytes = f.readAll();
    4.  
    5. qDebug() << bytes; //outputs ""MZP
    6.  
    7. qDebug() << bytes.toHex(); //Same output :(
    To copy to clipboard, switch view to plain text mode 
    Unpossible!

  20. #20
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by wysota View Post
    What doesn't work? What's the point of dumping the whole file to the screen in its hexadecimal form?

    Qt Code:
    1. #include <QFile>
    2. #include <stdio.h>
    3.  
    4. int main(int argc, char **argv) {
    5. QFile f(argv[1]);
    6. f.open(QFile::ReadOnly);
    7. int offset = 0;
    8. while(!f.atEnd()){
    9. QByteArray ba = f.read(16).toHex();
    10. QString offsetStr = QString::number(offset, 16);
    11. while(offsetStr.size()<8) offsetStr.prepend('0');
    12. printf("0x%s\t", qPrintable(offsetStr));
    13. offset+=16;
    14.  
    15. for(int i=ba.size()-2;i>0;i-=2) {
    16. ba.insert(i, ' ');
    17. }
    18.  
    19. printf("%s\n", ba.constData());
    20. }
    21. return 0;
    22. }
    To copy to clipboard, switch view to plain text mode 
    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....

Similar Threads

  1. Replies: 2
    Last Post: 9th June 2010, 17:08
  2. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 08:38
  3. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 5th July 2007, 00:12
  4. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 21:23
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.