gmseed
14th October 2009, 16:21
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
void testAddVertices(const QVector<VertexType>& vertices)
{
int numberVertices = vertices.size();
QByteArray byteArray;
QDataStream out(&byteArray,QIODevice::ReadWrite);
//double vx, vy, vz; // 64bit
float vx, vy, vz; // 32bit
for (int i=0; i<numberVertices; i++)
{
const VertexType& v = vertices[i];
vx = v.x(); vy = v.y(); vz = v.z();
out << vx; out << vy; out << vz;
}
byteArray = byteArray.toHex();
const char* data = byteArray.constData();
QString qdata(data);
std::string qdatas = qdata.toStdString();
std::cout << "qdata: " << qdatas << std::endl;
// extract
QByteArray byteArrayExtracted = QByteArray::fromHex(byteArray);
QBuffer buffer(&byteArrayExtracted);
//QBuffer buffer(&byteArray);
buffer.open(QIODevice::ReadOnly);
qint64 vertexComponentLength = 8;
qint64 vertexLength = 3*vertexComponentLength;
qint64 seekPos = 0;
qint64 numberBytesRead;
bool toOK, seekOK;
char* vxChar = new char[vertexComponentLength];
char* vyChar = new char[vertexComponentLength];
char* vzChar = new char[vertexComponentLength];
for (int i=0; i<numberVertices; i++)
{
numberBytesRead = buffer.read(vxChar,vertexComponentLength);
seekOK = buffer.seek(seekPos+vertexComponentLength);
numberBytesRead = buffer.read(vyChar,vertexComponentLength);
seekOK = buffer.seek(seekPos+2*vertexComponentLength);
numberBytesRead = buffer.read(vzChar,vertexComponentLength);
QByteArray vxCharArray(vxChar,vertexComponentLength);
//vxCharArray = QByteArray::fromHex(vxCharArray);
float vx = vxCharArray.toFloat(&toOK);
QByteArray vyCharArray(vyChar,vertexComponentLength);
//vyCharArray = QByteArray::fromHex(vyCharArray);
float vy = vyCharArray.toFloat(&toOK);
QByteArray vzCharArray(vzChar,vertexComponentLength);
//vzCharArray = QByteArray::fromHex(vzCharArray);
float vz = vzCharArray.toFloat(&toOK);
VertexType v(vx,vy,vz);
std::cout << "v(" << vx << ", " << vy << ", " << vz << ")" << std::endl;
// move to start of next vertex
seekPos += vertexLength;
}
// cleanup
delete vxChar; delete vyChar; delete vzChar;
}
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
void testAddVertices(const QVector<VertexType>& vertices)
{
int numberVertices = vertices.size();
QByteArray byteArray;
QDataStream out(&byteArray,QIODevice::ReadWrite);
//double vx, vy, vz; // 64bit
float vx, vy, vz; // 32bit
for (int i=0; i<numberVertices; i++)
{
const VertexType& v = vertices[i];
vx = v.x(); vy = v.y(); vz = v.z();
out << vx; out << vy; out << vz;
}
byteArray = byteArray.toHex();
const char* data = byteArray.constData();
QString qdata(data);
std::string qdatas = qdata.toStdString();
std::cout << "qdata: " << qdatas << std::endl;
// extract
QByteArray byteArrayExtracted = QByteArray::fromHex(byteArray);
QBuffer buffer(&byteArrayExtracted);
//QBuffer buffer(&byteArray);
buffer.open(QIODevice::ReadOnly);
qint64 vertexComponentLength = 8;
qint64 vertexLength = 3*vertexComponentLength;
qint64 seekPos = 0;
qint64 numberBytesRead;
bool toOK, seekOK;
char* vxChar = new char[vertexComponentLength];
char* vyChar = new char[vertexComponentLength];
char* vzChar = new char[vertexComponentLength];
for (int i=0; i<numberVertices; i++)
{
numberBytesRead = buffer.read(vxChar,vertexComponentLength);
seekOK = buffer.seek(seekPos+vertexComponentLength);
numberBytesRead = buffer.read(vyChar,vertexComponentLength);
seekOK = buffer.seek(seekPos+2*vertexComponentLength);
numberBytesRead = buffer.read(vzChar,vertexComponentLength);
QByteArray vxCharArray(vxChar,vertexComponentLength);
//vxCharArray = QByteArray::fromHex(vxCharArray);
float vx = vxCharArray.toFloat(&toOK);
QByteArray vyCharArray(vyChar,vertexComponentLength);
//vyCharArray = QByteArray::fromHex(vyCharArray);
float vy = vyCharArray.toFloat(&toOK);
QByteArray vzCharArray(vzChar,vertexComponentLength);
//vzCharArray = QByteArray::fromHex(vzCharArray);
float vz = vzCharArray.toFloat(&toOK);
VertexType v(vx,vy,vz);
std::cout << "v(" << vx << ", " << vy << ", " << vz << ")" << std::endl;
// move to start of next vertex
seekPos += vertexLength;
}
// cleanup
delete vxChar; delete vyChar; delete vzChar;
}