Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

create QByteArray::fromRawData with float getting unexpected results



  • Hey there,

    I have some question about a problem I just encountered. I am using QByteArray::fromRawData to send data via QSerialPort.
    This works fine for most of my data (some structs) but right now I am experiencing some results I can not explain:
    for example the call

    float myfloat = 500;
    auto tmp = QByteArray::fromRawData(reinterpret_cast<const char*>(&myfloat), sizeof(float));
    

    results in 0xc0d8001f
    instead of 0x43fa0000 as I would expect ( https://gregstoll.com/~gregstoll/floattohex/ )

    Does anyone know the reason for that and how I can get my result to work as expected? I am using this method call in a template to encode arbitrary data:

    auto sendBuffer = QByteArray::fromRawData(reinterpret_cast<const char*>(&sendItem), sizeof(T1));
    

    Thanks,


  • Lifetime Qt Champion

    @kain looks pretty sane, if you don't care for endianess.

    But note that fromRawData does not copy, so the float must stay in scope until the data is sent.



  • That solved indeed my problem! Thanks!


Log in to reply