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