Results 1 to 6 of 6

Thread: vector of doubles to QByteArray

  1. #1

    Default vector of doubles to QByteArray

    Hi

    I'm trying to convert a vector of doubles to a QByteArray; see code extract below.

    It appears to pack the vector of doubles ok but the toOK booleans in toFloat(&toOK) are all set to false when trying to unpack.

    Has anyone else ever done this operation before?

    Thanks for your help as this is driving me crazy.

    Graham

    Qt Code:
    1. void testAddVertices(const QVector<VertexType>& vertices)
    2. {
    3. int numberVertices = vertices.size();
    4.  
    5. QByteArray byteArray;
    6. QDataStream out(&byteArray,QIODevice::ReadWrite);
    7.  
    8. //double vx, vy, vz; // 64bit
    9. float vx, vy, vz; // 32bit
    10. for (int i=0; i<numberVertices; i++)
    11. {
    12. const VertexType& v = vertices[i];
    13. vx = v.x(); vy = v.y(); vz = v.z();
    14. out << vx; out << vy; out << vz;
    15. }
    16.  
    17. byteArray = byteArray.toHex();
    18. const char* data = byteArray.constData();
    19. QString qdata(data);
    20. std::string qdatas = qdata.toStdString();
    21. std::cout << "qdata: " << qdatas << std::endl;
    22.  
    23. // extract
    24. QByteArray byteArrayExtracted = QByteArray::fromHex(byteArray);
    25. QBuffer buffer(&byteArrayExtracted);
    26. //QBuffer buffer(&byteArray);
    27. buffer.open(QIODevice::ReadOnly);
    28. qint64 vertexComponentLength = 8;
    29. qint64 vertexLength = 3*vertexComponentLength;
    30. qint64 seekPos = 0;
    31. qint64 numberBytesRead;
    32. bool toOK, seekOK;
    33. char* vxChar = new char[vertexComponentLength];
    34. char* vyChar = new char[vertexComponentLength];
    35. char* vzChar = new char[vertexComponentLength];
    36. for (int i=0; i<numberVertices; i++)
    37. {
    38. numberBytesRead = buffer.read(vxChar,vertexComponentLength);
    39.  
    40. seekOK = buffer.seek(seekPos+vertexComponentLength);
    41. numberBytesRead = buffer.read(vyChar,vertexComponentLength);
    42.  
    43. seekOK = buffer.seek(seekPos+2*vertexComponentLength);
    44. numberBytesRead = buffer.read(vzChar,vertexComponentLength);
    45.  
    46. QByteArray vxCharArray(vxChar,vertexComponentLength);
    47. //vxCharArray = QByteArray::fromHex(vxCharArray);
    48. float vx = vxCharArray.toFloat(&toOK);
    49. QByteArray vyCharArray(vyChar,vertexComponentLength);
    50. //vyCharArray = QByteArray::fromHex(vyCharArray);
    51. float vy = vyCharArray.toFloat(&toOK);
    52. QByteArray vzCharArray(vzChar,vertexComponentLength);
    53. //vzCharArray = QByteArray::fromHex(vzCharArray);
    54. float vz = vzCharArray.toFloat(&toOK);
    55.  
    56. VertexType v(vx,vy,vz);
    57. std::cout << "v(" << vx << ", " << vy << ", " << vz << ")" << std::endl;
    58.  
    59. // move to start of next vertex
    60. seekPos += vertexLength;
    61. }
    62.  
    63. // cleanup
    64. delete vxChar; delete vyChar; delete vzChar;
    65. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector of doubles to QByteArray

    Hi.
    Why you don't use QDataStream to unpack?

    Your problème is maybe linked with the endian or float représentation use to write float to QByteArray
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

  3. #3

    Default Re: vector of doubles to QByteArray

    Hi

    QDataStream feeds through to the underlying QByteArray so I chose to operate on that directly.

    Graham

  4. #4
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector of doubles to QByteArray

    QDataSTream work in big endian by default.
    http://doc.trolltech.com/4.6-snapsho...ByteOrder-enum


    try qFromBigEndian to convert endian on the float readed.
    http://doc.trolltech.com/4.6-snapsho...romBigEndian-2
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

  5. #5

    Default Re: vector of doubles to QByteArray

    Hi

    The only way I could et it to work is to work through QDataStream for both << and >>; see pasted code below.

    I need to index into the packed byte array and found:

    QIODevice* device = in.device();

    works on the underlying internal QBuffer.

    As illustrated in the first post, if I operate on the QBuffer outside of the QDataStream I can't get it to work.

    It's a strange one and can only think that if QDataStream is used throughout then the encoding is consistent.

    Cheers

    Graham



    Qt Code:
    1. void testAddVertices(const QVector<VertexType>& vertices)
    2. {
    3. int numberVertices = vertices.size();
    4.  
    5. QByteArray byteArray;
    6. QDataStream out(&byteArray,QIODevice::WriteOnly);
    7.  
    8. //double vx, vy, vz; // 64bit
    9. float vx, vy, vz; // 32bit
    10. for (int i=0; i<numberVertices; i++)
    11. {
    12. const VertexType& v = vertices[i];
    13. vx = v.x(); vy = v.y(); vz = v.z();
    14. out << vx; out << vy; out << vz;
    15. }
    16.  
    17. std::cout << "byteArray length: " << byteArray.length() << std::endl;
    18.  
    19. // extract
    20. QDataStream in(&byteArray,QIODevice::ReadOnly);
    21. QIODevice* device = in.device();
    22. qint64 vertexComponentLength = 4;
    23. qint64 vertexLength = 3*vertexComponentLength;
    24. qint64 seekPos = 0;
    25. qint64 numberBytesRead;
    26. bool toOK, seekOK;
    27. float currentValue;
    28. //float vx, vy, vz;
    29. for (int i=0; i<numberVertices; i++)
    30. {
    31. // seek to start of new vertex
    32. device->seek(seekPos);
    33. in >> vx;
    34.  
    35. device->seek(seekPos+vertexComponentLength);
    36. in >> vy;
    37.  
    38. device->seek(seekPos+2*vertexComponentLength);
    39. in >> vz;
    40.  
    41. VertexType v(vx,vy,vz);
    42. std::cout << "v(" << vx << ", " << vy << ", " << vz << ")" << std::endl;
    43.  
    44. // move to start of next vertex
    45. seekPos += vertexLength;
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector of doubles to QByteArray

    For me this work perfectly

    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5.  
    6. QByteArray byteArray;
    7.  
    8. //generate ten random float value
    9. QVector<float> v;
    10. for (int i =0; i < 10;++i)
    11. {
    12. v << 100. * qrand()/RAND_MAX;
    13. }
    14. //ouput vector
    15. qDebug() << v;
    16.  
    17. //write float value to array
    18. {
    19. QDataStream out (&byteArray,QIODevice::WriteOnly);
    20. foreach(float f,v)
    21. {
    22. out << f;
    23. }
    24. }
    25.  
    26. //read all float from array
    27. QVector<float> v2;
    28. {
    29. QDataStream in(byteArray);
    30. while (!in.atEnd())
    31. {
    32. float f;
    33. in >> f;
    34. v2 << f;
    35. }
    36. }
    37. //ouput vector
    38. qDebug() << v2;
    39.  
    40.  
    41. return 0;
    42. }
    To copy to clipboard, switch view to plain text mode 
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

Similar Threads

  1. storing integer 4bytes in QByteArray
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 30th November 2008, 10:08
  2. QByteArray problem
    By Misenko in forum Qt Programming
    Replies: 17
    Last Post: 4th October 2008, 21:53
  3. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 17:53
  4. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23

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.