Unsolved Read binary file by block
-
Hi,
I'm new of Qt. I need to perform the following steps:- Read a binary file (extension unknown) by block of fixed dimension.
- Append each block to a QStringList object (cannot change object type) in order to send it to a remote server
- On the remote server (wrote in C code) convert back the binary file without information lost.
I perform the following step:
QVector<char*> vect;
if (! m_fi.open(QIODevice::ReadOnly)){
return;
}
while(!m_fi.atEnd())
{
vect[idx] = new char[BLOCK_DIMENSION];
m_fi.read(vect[idx],BLOCK_DIMENSION);
}QStringList parameters;
parameters.append(vect[idx]);
/* Send "parameters" to remote server */(On the remore C server)
How I can return back to the original binary file?Many thanks
Sarah -
Hi and welcome to the forums
Are you sure QString will handle the binary data well ?
Normally i use QByteArray as not to get into issues with NULL values.Anyway, if we assume that the data survives and
the server is using SAME Endian Byte Order as you client.
https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/Then you can just open a file on the server and write back the blocks and get the file.
-
@mrjj said in Read binary file by block:
ByteArray
Hi,
really thanks for your reply.
Where do you meant to use QByteArray? inside QVector instead of char*?And then, how I may append a QByteArray in a QStringList?
Thanks
Sarah -
Well it would be instead of QStringList.
Anyway, docs says
http://doc.qt.io/qt-5/qstring.html#details
That it can handle zeros might just work.I would for test just store the QStringList to binary file (on the client) and see if it matches the
binary file, you use for input.Is this a real application or more like a school project ?
Normally one would use
http://doc.qt.io/qt-5/qdatastream.html
as it handle byte order and is meant for binary data.
https://stackoverflow.com/questions/26146445/how-to-send-data-from-server-to-client-as-qbytearray-qdatastream/46162082#46162082Also why do need to manually split the input ?
-
@cerbica said in Read binary file by block:
And then, how I may append a QByteArray in a QStringList?
You can use the base64 representation of the array to make sure it's all ascii data.
stringList.append(QString::fromLatin1(byteArray.toBase64()));
-
@VRonin said in Read binary file by block:
stringList.append(QString::fromLatin1(byteArray.toBase64()));
Hi,
are you able to suggest me how to turn back to the binary string on the C server side?Thanks
Sarah -
To decode the base64 you can use https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c
No idea how you can reliably deserialise a QString object on the C side though as I think that's an implementation detail (i.e. Qt can change it without warning from one version to the next)
-
-
@cerbica said in Read binary file by block:
Now I have to turn back to the original binary string on C server side
Did you already receive it? in what format are you storing it C side?