Results 1 to 10 of 10

Thread: Reading value from hex address in QT

  1. #1
    Join Date
    Sep 2016
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question Reading value from hex address in QT

    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:
    Qt Code:
    1. QFile file ("example.dat";
    2. QString data = "30";
    3.  
    4. file.open(QIODevice::ReadWrite);
    5. QByteArray fileData(settings.readAll());
    6.  
    7. QByteArray data_write = QByteArray::fromHex(data.toLatin1());
    8. fileData.replace(0x00000008,1,data_write);
    9.  
    10. file.seek(0);
    11. file.write(fileData);
    12. file.flush();
    13. file.close();
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by JohnnyUSA; 13th October 2016 at 12:02.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading value from hex address in QT


  3. The following user says thank you to Lesiok for this useful post:

    JohnnyUSA (13th October 2016)

  4. #3
    Join Date
    Sep 2016
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading value from hex address in QT

    Quote Originally Posted by Lesiok View Post
    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!

    Qt Code:
    1. QFile file ("example.dat";
    2. file.open(QIODevice::ReadOnly);
    3. file.seek(0x00000018);
    4.  
    5. QByteArray bytes = settings.read(1);
    6. file.flush();
    7. file.close();
    8.  
    9. qDebug() << bytes.toHex();
    To copy to clipboard, switch view to plain text mode 

    Thanks again! A beer on you.
    Last edited by JohnnyUSA; 13th October 2016 at 12:46.

  5. #4
    Join Date
    Sep 2016
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading value from hex address in QT

    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:
    Qt Code:
    1. const int horizontal = qFromLittleEndian<qint16>(reswidth.data())
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. int width_int = width->value();
    2. QString width_string = QString::number(width_int, 16);
    3.  
    4. QFile settings("example.dat");
    5.  
    6. settings.open(QIODevice::ReadWrite);
    7. QByteArray fileData(settings.readAll());
    8.  
    9. QByteArray hex1 = QByteArray::fromHex(width_string.toLatin1());
    10. fileData.replace(0x0000000C,2, hex1);
    11. settings.seek(0);
    12. settings.write(fileData);
    13. settings.flush();
    14. settings.close();
    15.  
    16. qDebug() << hex1;
    To copy to clipboard, switch view to plain text mode 

    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!

  6. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading value from hex address in QT

    Strange, you know the function of qFromLittleEndian and you do not know qToLittleEndian ?

  7. #6
    Join Date
    Sep 2016
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading value from hex address in QT

    Quote Originally Posted by Lesiok View Post
    Strange, you know the function of qFromLittleEndian and you do not know qToLittleEndian ?
    i've tried:
    Qt Code:
    1. qDebug() << qToLittleEndian(hex1);
    To copy to clipboard, switch view to plain text mode 

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

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading value from hex address in QT

    And what is the output from :
    Qt Code:
    1. tab.resize(4);
    2. qToLittleEndian(width_int,tab.data());
    3. qDebug() << tab;
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Lesiok for this useful post:

    JohnnyUSA (14th October 2016)

  10. #8
    Join Date
    Sep 2016
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading value from hex address in QT

    Quote Originally Posted by Lesiok View Post
    And what is the output from :
    Qt Code:
    1. tab.resize(4);
    2. qToLittleEndian(width_int,tab.data());
    3. qDebug() << tab;
    To copy to clipboard, switch view to plain text mode 
    It's ""\x80\x02\x00\x00""

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

  11. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading value from hex address in QT

    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.

  12. The following user says thank you to Lesiok for this useful post:

    JohnnyUSA (14th October 2016)

  13. #10
    Join Date
    Sep 2016
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading value from hex address in QT

    Quote Originally Posted by Lesiok View Post
    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

Similar Threads

  1. getting IP-address
    By jefklak in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2010, 09:19
  2. IP Address Validation
    By rk0747 in forum Qt Programming
    Replies: 5
    Last Post: 10th June 2010, 08:18
  3. how can i get IP address
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2009, 05:27
  4. IP Address
    By rajveer in forum Qt Programming
    Replies: 2
    Last Post: 11th November 2008, 08:30
  5. validation on an IP address
    By rajveer in forum Qt Programming
    Replies: 15
    Last Post: 24th August 2008, 00:35

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.