Results 1 to 7 of 7

Thread: Reading binary into QVector

  1. #1
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Reading binary into QVector

    Hi,
    I am trying to read binary data into a QVector. Each chunk is represented as 2 Bytes (unsigned int).

    The follow code works:

    QDataStream in(m_tcpSocket);
    if (m_tcpSocket->bytesAvailable() < m_dataLength ) return;

    qint16 a;
    QVector<double> dataVector;

    for(int i=0; i<m_dataLength; i++)
    {
    in >> a;
    dataVector.push_back(a);
    }

    The first five values that I get from this code are what I expect:
    0 3 0 16384 -7460 -7483

    But I want to avoid the loop and I'd like to get the pointer to the beginning of the data and to the end of the data and then transfer this into the memory and put it into a vector.
    I tried the following:

    char *s = new char[ 5 * sizeof(qint16)]; //Get only the first 8 bytes
    in.readRawData( s , 5*sizeof(qint16) );

    QVector<qint16> vector;
    qint16 *vdata = vector.data();
    vdata = (qint16*)s;

    If I print out the first 5 values of the vector I get:
    -21589 -21589 -21589 -21589 0

    Has anyone done this before?
    I'd appreciate any contribution.

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading binary into QVector

    Qt Code:
    1. QVector<qint16> vector;
    2. qint16 *vdata = vector.data();
    3. vdata = (qint16*)s;
    To copy to clipboard, switch view to plain text mode 
    Hmm this is NOT going to work :]
    You create vdata pointer that points to vector data. But then you make it pointing to the s array. So how the vector data can be changed, if there is even no line making any change to it.
    Moreover, I think all your method is not going to work. You can try with memcpy to copy data to the memory pointed with vector.data() pointer, but there is great chance you do some segmentation fault (hmm what is the size of reserved memory for vector, which you get by vector.data() method?).

    So either stay with QVector (or QList) and with a loop or just do everything with low level C arrays and do some harcore memcpys and other stuff, and don't mess vector internal structures :P
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Reading binary into QVector

    I agree with faldzip, but just for fun I would try.

    QVector<qint16> vector;
    vector.reserve(6);//one extra for safety.
    in.readRawData((char*)vector.data() , 5*sizeof(qint16) );

    sizeof might be redundant here. from qt assistant:

    typedef qint16
    Typedef for signed short. This type is guaranteed to be 16-bit on all platforms supported by Qt.

  4. #4
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading binary into QVector

    Thanks both for your replies.
    I tried the following

    QVector<qint16> vector;
    vector.reserve(6);
    in.readRawData((char*)vector.data(), 5*sizeof(qint16) );
    for(int i=0; i<5; i++)
    {
    qDebug()<<vector[i];
    }

    And now I get:
    0 768 0 64 -8990

    where I should get:
    0 3 0 16384 -7460

    There must be a conversion problem I think. As you see I can get the zeros and the sign but not the correct number.. Is there a possibility the bytes to be read the inverse order?
    Thanks

  5. #5
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Reading binary into QVector

    this seems to be working:

    Qt Code:
    1. //setup
    2. QVector<qint16> inVect;
    3. inVect << 0 << 3 << 0 << 16384 << -7460;
    4. QByteArray storage;
    5. QDataStream storageIn(&storage, QIODevice::WriteOnly);
    6. storageIn.writeRawData((const char*)inVect.data(), 10);
    7. //read back
    8. QDataStream storageOut(&storage, QIODevice::ReadOnly);
    9. QVector<qint16> outVect;
    10. outVect.resize(5);
    11. storageOut.readRawData((char*)outVect.data(), 10);
    12. foreach (qint16 current, outVect)
    13. qDebug() << current;
    To copy to clipboard, switch view to plain text mode 

    output is:
    Starting .../QtConsoleTest...
    0
    3
    0
    16384
    -7460

    .../QtConsoleTest exited with code 0

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

    stathis.stefanidis (16th December 2009)

  7. #6
    Join Date
    Aug 2009
    Posts
    52
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading binary into QVector

    Quote Originally Posted by tanderson View Post
    this seems to be working:

    Qt Code:
    1. //setup
    2. QVector<qint16> inVect;
    3. inVect << 0 << 3 << 0 << 16384 << -7460;
    4. QByteArray storage;
    5. QDataStream storageIn(&storage, QIODevice::WriteOnly);
    6. storageIn.writeRawData((const char*)inVect.data(), 10);
    7. //read back
    8. QDataStream storageOut(&storage, QIODevice::ReadOnly);
    9. QVector<qint16> outVect;
    10. outVect.resize(5);
    11. storageOut.readRawData((char*)outVect.data(), 10);
    12. foreach (qint16 current, outVect)
    13. qDebug() << current;
    To copy to clipboard, switch view to plain text mode 

    output is:
    Starting .../QtConsoleTest...
    0
    3
    0
    16384
    -7460

    .../QtConsoleTest exited with code 0
    You can try this:
    Qt Code:
    1. QVector<qint16> inVect;
    2. inVect << 0 << 3 << 0 << 16384 << -7460;
    3. qDebug()<<QByteArray((const char*)inVect.data(), 10).toHex();
    4. inVect.clear();
    5. inVect << 0 << 768 << 0 << 64 << -8990;
    6. qDebug()<<QByteArray((const char*)inVect.data(), 10).toHex();
    To copy to clipboard, switch view to plain text mode 
    output:
    Qt Code:
    1. "0000030000000040dce2"
    2. "0000000300004000e2dc"
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Reading binary into QVector

    I finally made it work using the idea by tanderson..
    Thanks to all for contributing!

Similar Threads

  1. Binary file reading using Structure
    By umulingu in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2009, 11:35
  2. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18
  3. reading 4-bytes integer from binary file
    By maka in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2009, 05:57
  4. QFile, QDataStream reading binary data
    By yren in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2009, 06:34
  5. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53

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.