0

I would like to convert QVector to QByteArray. I am using something like this:

QByteArray bytesArray
QDataStream out(&bytesArray ,QIODevice::WriteOnly);
out << someVector;
bytesArray = qCompress(bytesArray, 5);

And to read it:

bytesArray = qUncompress(bytesArray)
QDataStream in(bytesArray);
QVector<qreal> otherVector;
in >> otherVector;

But i have a problem. I saved number like 1.23124121242135, and after reading i got 1.23124. How can i store and read full number?

  • 1
    Please provide a [mcve] that shows how you obtained these numbers. Are you doing something that more or less equates to `qreal r = 1.23124121242135` followed by `std::cout << r` (`QDataStream`s and compression aside)? If so you might want to look at ["Is floating point math broken?"](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – G.M. Jan 13 '20 at 19:36

1 Answers1

0

If for any reason it wont allow storing the value( e.g.. 0.0 +10 or more decimals u could do a workaround 1 convert double t string and get t_size to int 2 create a long or another int according to the length of said value and multiply it with the size of original and save size with the new int value.

If original is 1.876649018 size is 10 so the multiplier is 10^10

3 getting it back is just a matter of division of new long int value with the size

That is the only thing that comes to mind right now...

Dressy Fiddle
  • 163
  • 10