Results 1 to 20 of 37

Thread: Can somebody show how to read bytes?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


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

  3. #3
    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!!!

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

  5. #5
    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!!!

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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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 09: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.


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

  9. #9
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by "BumbleBee" View Post
    .btw,your code makes an infinite loop....
    where is that infinite loop? I don't see any...

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

    Default Re: Can somebody show how to read bytes?

    Qt Code:
    1. #include "StdAfx.h"
    2. #include <iostream>
    3. #include <string.h>
    4. using namespace std;
    5.  
    6. void encrypt_data();
    7.  
    8. int main()
    9. {
    10. FILE * f;
    11. unsigned char buffer[100];
    12. int n;
    13.  
    14. f = fopen("C:\\Program Files (x86)\\TeamViewer\\Version6\\TeamViewer.exe", "rb");
    15. if (f)
    16. {
    17. n = fread(buffer, 100, 1, f);
    18. for (int c = 0; c <= 100 ; c++)
    19. {
    20. printf("%.2X ", (int)buffer[c]); //outputs hex!!!!!!
    21. // put an extra space between every 4 bytes
    22. if (c % 4 == 3)
    23. {
    24. printf(" ");
    25. }
    26.  
    27. // Display 16 bytes per line
    28. if (c % 16 == 15)
    29. {
    30. printf("\n");
    31. }
    32. }
    33. // Add an extra line feed for good measure
    34. printf("\n\n\n");
    35. }
    36. else
    37. {
    38. cout << "error";
    39. }
    40.  
    41. char a;
    42. cin >> a;
    43. return 0;
    44. }
    To copy to clipboard, switch view to plain text mode 

    I want to make that in Qt^^^

    And FelixB the loop doesn't stop for me.

  11. #11
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by "BumbleBee" View Post
    And FelixB the loop doesn't stop for me.
    which loop do you mean?

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

    Default Re: Can somebody show how to read bytes?

    it keeps outputting bytes....never stops.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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
    btw,your code makes an infinite loop....
    There is no infinite loop in my code.
    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.


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

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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?

    Maybe he wants to encrypt twice as much as he needs to? It's always fun to make a 10MB file out of a 5MB file...
    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.


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

    Default Re: Can somebody show how to read bytes?

    Quote Originally Posted by squidge View Post
    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?
    Qt Code:
    1. QByteArray byte = file.read(16);
    To copy to clipboard, switch view to plain text mode 

  17. #17
    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
    You mean like this?
    Qt Code:
    1. QByteArray byte = file.read(16);
    To copy to clipboard, switch view to plain text mode 
    Yes, exactlly.

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

    Default Re: Can somebody show how to read bytes?

    This outputs some strange symbols(I suppose bin. code),but it also beeps and crashes after some secs...

  19. #19
    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!!!

  20. #20
    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!

Similar Threads

  1. Replies: 2
    Last Post: 9th June 2010, 16:08
  2. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 07:38
  3. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 4th July 2007, 23:12
  4. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 20: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, 14: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
  •  
Qt is a trademark of The Qt Company.