Results 1 to 5 of 5

Thread: Porting from C# to Qt4

  1. #1
    Join Date
    Sep 2010
    Location
    Zamość, Poland
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Symbian S60

    Default Porting from C# to Qt4

    Hello.
    I'm porting some code from C# to Qt4 and I met some problems.
    The program opens a binary file and retrieves some data from it.

    This is code in C#:

    Qt Code:
    1. string scriptName = args[0];
    2. Encoding jis = Encoding.GetEncoding(932);
    3. using(BinaryReader script = new BinaryReader(scriptFile.OpenRead(), jis))
    4. {
    5. script.BaseStream.Seek(0x020, SeekOrigin.Begin);
    6. uint opcode = script.ReadUInt32();
    7. uint codeSize = script.ReadUInt32();
    8.  
    9. script.BaseStream.Seek(0, SeekOrigin.Begin);
    10. byte[] scriptBuffer = script.ReadBytes((int)codeSize);
    11.  
    12. for(int dwptr = 0; dwptr < codeSize; dwptr += 4)
    13. {
    14. uint currentDw = scriptBuffer[dwptr];
    15. if(currentDw != 0x03 && currentDw != 0x07F)
    16. continue;
    17.  
    18. dwptr += 4;
    19. currentDw = ToUInt32(scriptBuffer, dwptr);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    and reimplemented ToUInt32 (in C#):
    Qt Code:
    1. static uint ToUInt32(byte[] array, int beginOfs)
    2. {
    3. return(uint)((array[beginOfs+3] << 24) | (array[beginOfs+2] << 16) | (array[beginOfs+1] << 8) | (array[beginOfs]));
    4. }
    To copy to clipboard, switch view to plain text mode 

    I ported this code to:

    Qt Code:
    1. QFile cScript(fPath+"/"+fName);
    2. cScript.open(QIODevice::ReadOnly);
    3.  
    4. QDataStream cData(&cScript);
    5. cData.setByteOrder(QDataStream::LittleEndian);
    6.  
    7. cScript.seek(0x020);
    8.  
    9. quint32 opcode;
    10. quint32 codeSize;
    11.  
    12. cData >> opcode;
    13. cData >> codeSize;
    14.  
    15. cScript.seek(0);
    16. QByteArray fragment = cScript.read(codeSize);
    17.  
    18. for(quint32 dwptr = 0; dwptr < codeSize; dwptr += 4)
    19. {
    20. quint32 curr = fragment[dwptr];
    21. if(curr != 0x03 && curr != 0x07F)
    22. continue;
    23.  
    24. dwptr +=4;
    25. curr = toUint32(fragment, dwptr);
    26. }
    To copy to clipboard, switch view to plain text mode 

    and toUint32 function:

    Qt Code:
    1. quint32 EustiaTools::toUint32(QByteArray array, uint offStart)
    2. {
    3. return(quint32)((array[offStart+3] << 24) | (array[offStart+2] << 16) | (array[offStart+1] << 8) | (array[offStart]));
    4. }
    To copy to clipboard, switch view to plain text mode 

    My problem probably lies in this line:

    Qt Code:
    1. quint32 curr = fragment[dwptr];
    To copy to clipboard, switch view to plain text mode 

    And in function toUint32 too.
    I checked and my curr before for loop equals 4294967272 when currentDw from C# equals 232.
    I changed type of curr from quint32 to int and then curr equals -24.

    How to repair that?

    Sorry for my bad english.
    Thanks in advance.

  2. #2
    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: Porting from C# to Qt4

    Your problem lies in the use of QDataStream. It's not an equivalent of BinaryReader but rather a serialization mechanism. Use QFile only and forget about QDataStream.
    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.


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

    Sölve (14th May 2011)

  4. #3
    Join Date
    Sep 2010
    Location
    Zamość, Poland
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Porting from C# to Qt4

    Thank you for your reply.
    I replaced getting data from QDataStream by memcpy and changed line:
    Qt Code:
    1. quint32 curr = fragment[dwptr];
    To copy to clipboard, switch view to plain text mode 
    to:
    Qt Code:
    1. quint32 curr;
    2. memcpy(&curr, QByteArray(1, fragment[dwptr]).constData(), 4);
    To copy to clipboard, switch view to plain text mode 

    Now my code looks like that:
    Qt Code:
    1. QFile cScript(fPath+"/"+fName);
    2. cScript.open(QIODevice::ReadOnly);
    3.  
    4. quint32 opcode;
    5. quint32 codeSize;
    6.  
    7. cScript.seek(0x020);
    8. memcpy(&opcode, cScript.read(4).constData(), 4);
    9. memcpy(&codeSize, cScript.read(4).constData(), 4);
    10.  
    11. cScript.seek(0);
    12. QByteArray fragment = cScript.read(codeSize);
    13.  
    14. for(quint32 dwptr = 0; dwptr < codeSize; dwptr += 4)
    15. {
    16. quint32 curr;
    17. memcpy(&curr, QByteArray(1, fragment[dwptr]).constData(), 4);
    18. qDebug() << curr;
    19.  
    20. if(curr != 0x03 && curr != 0x07F)
    21. continue;
    22.  
    23. dwptr +=4;
    24. curr = toUint32(fragment, dwptr);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Now I've got bad first value of curr, but rest of values are ok.
    First value = 3342352, but I need 16.
    Is it related to memcpy() function?

  5. #4
    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: Porting from C# to Qt4

    My guess is you want to read a single byte and not 32 bits, that's what you seem to be doing in your original code. QByteArray::at() will return a single byte for you.
    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.


  6. The following user says thank you to wysota for this useful post:

    Sölve (15th May 2011)

  7. #5
    Join Date
    Sep 2010
    Location
    Zamość, Poland
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Symbian S60

    Default [Solved] Porting from C# to Qt4

    Okay, I rewrote code and changed quint32 to quint8 which fits to value returned by QByteArray::at(). Thanks for help, topic solved.

Similar Threads

  1. Porting qt3 to qt4
    By divya balachandran in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2008, 00:49
  2. Porting Qt3 To Qt4
    By fruzzo in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2008, 15:59
  3. Porting from qwt-4.2.0 to qwt-5.0.2
    By vheinitz in forum Qwt
    Replies: 3
    Last Post: 31st January 2008, 13:39
  4. Porting from Qt3 to Qt4
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2007, 06:27
  5. Porting from Qt3 to Qt4
    By Opilki_Inside in forum Installation and Deployment
    Replies: 1
    Last Post: 1st February 2006, 19:20

Tags for this Thread

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.