Convert from float to QByteArray

后端 未结 2 1005
慢半拍i
慢半拍i 2021-01-02 23:16

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 u

2条回答
  •  粉色の甜心
    2021-01-03 00:18

    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(&f), sizeof (f));
    

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

    QByteArray baHex = ba.toHex();
    

提交回复
热议问题