Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. create QByteArray::fromRawData with float getting unexpected results
Forum Updated to NodeBB v4.3 + New Features

create QByteArray::fromRawData with float getting unexpected results

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 466 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kain
    wrote on last edited by
    #1

    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,

    aha_1980A 1 Reply Last reply
    0
    • K kain

      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,

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      @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.

      Qt has to stay free or it will die.

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kain
        wrote on last edited by
        #3

        That solved indeed my problem! Thanks!

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved