float to byte array



  • Hello,
    I am using qt4.8.

    I wanted to convert a float value into byte array.
    eg. float val = 20000
    val in hex = 46 9c 40 00 (00 40 9c 46 Little Endian)
    val in int = 70 156 64 00 (00 64 156 70)
    So basically I want the value (val in int) to be in array. ii.e array[0] = 00 , array [1] = 64, array[2] = 156, array[3] = 70.


  • Qt Champions 2016

    @sandycoolxyz said in float to byte array:

    union  {
        float val;
        char * array;
    } conversion;
    
    conversion.val = 20000;
    QByteArray byteArray = QByteArray::fromRawData(conversion.array, sizeof(float));
    

    or

    float val = 20000;
    QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float));
    

    or

    float val = 20000;
    QByteArray byteArray;
    QDataStream out(&byteArray);
    out << val;
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.