Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 261 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert from float to QByteArray

#1
Is there a quick way to convert a float value to a byte wise (hex) representation in a `QByteArray`?

Have done similar with `memcpy()` before using arrays, but this doesn't seem to work too well with `QByteArray`.

For example:

memcpy(&byteArrayData,&floatData,sizeof(float));

Can go the other way just fine using:

float *value= (float *)byteArrayData.data();


Am I just implementing this wrong or is there a better way to do it using Qt?

Thanks
Reply

#2
I'm not sure what you want exactly.

To stuff the binary representation into a QByteArray you can use this:

float f = 0.0f;
QByteArray ba(reinterpret_cast<const char *>(&f), sizeof (f));

To get a hex representation of the float you can add this:

QByteArray baHex = ba.toHex();
Reply

#3
From the [QByteArray Class Reference][1] page:

float f = 0.0f;
QByteArray array(reinterpret_cast<const char*>(&f), sizeof(f));


Will initialize a `QByteArray` with the memory content of the float stored in it.

If you already have one and just want to append the data to it:

array.append(reinterpret_cast<const char*>(&f), sizeof(f));

Should do it as well.

To go the other way around, you just have to perform the reverse operation:

float f2;

if (array.size() >= sizeof(f2)
{
f2 = *reinterpret_cast<const float*>(array.data());
} else
{
// The array is not big enough.
}


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through