Results 1 to 17 of 17

Thread: conversion of binay data to qt types

  1. #1
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up conversion of binay data to qt types

    Hi All,

    I am using Qt 4.3 ,

    I am having binary data in my file, I am reading that data into QByteArray. I want to get some integers from that data ,

    So i am using QByteArray.toInt(). But i am not getting proper int values,

    How to change binary data into other types ?

    please help me.
    Thanks,
    Rajesh.S

  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: conversion of binay data to qt types

    for that you need to know in advance how the data is structured in the file. (serialized)
    If you know that, you can just read the values directly in their right types (de-serialization).
    ==========================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
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: conversion of binay data to qt types

    in my case file may be in any structures, i need to get whole data as String or otherwise

    i need some integers that may be placed in some particular position in the file,

    that may be in middle , how to handle this type of suituations in Qt?

    please help me.
    Thanks,
    Rajesh.S

  4. #4
    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: conversion of binay data to qt types

    Lets make order in things.
    If you need to serialize data IN to a file, just use QDataStream and write the data to the file.
    But you have to know how you did that in order to be able to read that data back.
    When you read the data back, you can do it exactly as you did when you wrote it, using QDataStream- directly in the correct types, no need to convert.

    You can't read binary data from a file, unless you know how it was written.
    Well, you can READ it, but converting it to something understandable will be guess work.
    ==========================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.

  5. #5
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: conversion of binay data to qt types

    I tried to read whole data usiing QDataStream >> oprator with char * ;

    My code is like this ,

    Qt Code:
    1. QFile file("binary.ked");
    2. file.open(QIODevice::ReadWrite);
    3. QDataStream ds(&file);
    4. uint i = file.size();
    5. char *ch = new char[i];
    6. ds>>ch;
    To copy to clipboard, switch view to plain text mode 

    if i tried to debug the code i am getting bad pointer for *ch ?
    Is there any mistake in my code?
    Last edited by jpn; 3rd January 2008 at 11:29. Reason: changed [b] to [code]
    Thanks,
    Rajesh.S

  6. #6
    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: conversion of binay data to qt types

    QDataStream & QDataStream:perator>> ( char *& s )

    This is an overloaded member function, provided for convenience.

    Reads the '\0'-terminated string s from the stream and returns a reference to the stream.

    Space for the string is allocated using new -- the caller must destroy it with delete[].
    If the data in the file is not a '\0' string.
    Probably you don't have a '\0' in the data, resulting in a NULL pointer.
    Try using QDataStream::readRawData()

    note the last line in the docs!


    But what it is you are trying to do?
    Do you know how the file was written?
    If so, you can just de-serialize it.
    Last edited by high_flyer; 3rd January 2008 at 11:42.
    ==========================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.

  7. #7
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: conversion of binay data to qt types

    Sorry, I tried even readRawData instead readBytes, I am getting same BadPtr.


    even i tried to add \0 also at the end of the file , i am gettiing same BadPtr.
    Thanks,
    Rajesh.S

  8. #8
    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: conversion of binay data to qt types

    can you post your code?
    what is 'i' at the time you allocate your char array? is it > 0 ?
    ==========================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.

  9. #9
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: conversion of binay data to qt types

    Whatever code i posted previously is the correct code,

    I am giving down ,

    i is size() of file,

    QFile file("binary.ked");
    file.open(QIODevice::ReadWrite);
    QDataStream ds(&file);
    uint i = file.size();
    char *ch = new char[i]; ds>>ch;


    I tried even some other values like ( 1,2,3,4,5) for i , its giving same o/p ?

    please help me,
    Thanks,
    Rajesh.S

  10. #10
    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: conversion of binay data to qt types

    what I meant was does size() returns a positive value?
    Try this:
    Qt Code:
    1. QFile file("binary.ked");
    2. if(file.open(QIODevice::ReadWrite))
    3. {
    4. QDataStream ds(&file);
    5. uint i = file.size();
    6. if(i>0)
    7. {
    8. char *ch = new char[i];
    9. ds.readRawData(ch,i);
    10. }
    11. else qDebug()<<"size is null";
    12. }
    13. else qDebug()<<"could not open file";
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  11. #11
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: conversion of binay data to qt types

    ya it's returning +ve value , still same prob.
    Thanks,
    Rajesh.S

  12. #12
    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: conversion of binay data to qt types

    did you try the code I suggested?
    ==========================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.

  13. #13
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: conversion of binay data to qt types

    yes , i tried ur code still its going to true part and giving the same.
    Thanks,
    Rajesh.S

  14. #14
    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: conversion of binay data to qt types

    strange.
    Try adding:
    Qt Code:
    1. if(ch != NULL)
    To copy to clipboard, switch view to plain text mode 
    before
    Qt Code:
    1. ds.readRawData(ch,i);
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  15. #15
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: conversion of binay data to qt types

    if i use readRawData i am getting some binary data , but if i use readBytes i am getting bad pointer , i need to get string data in ordinary format not in binary format , how to achieve this ?
    Last edited by rajeshs; 4th January 2008 at 03:51.
    Thanks,
    Rajesh.S

  16. #16
    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: conversion of binay data to qt types

    f i use readRawData i am getting some binary data
    but before you said you got still the bad pointer?
    What did you change?
    ==========================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.

  17. #17
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: conversion of binay data to qt types

    sorry ,since i need binary data as normal data i tried to use only readbytes and >> operator ,
    Thanks,
    Rajesh.S

Similar Threads

  1. system-independent C++ data types
    By magland in forum General Programming
    Replies: 15
    Last Post: 28th March 2007, 20:33
  2. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 15:36
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  4. Use QVariant with custon data types
    By mcosta in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 14:55

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.