Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. float to byte array
Forum Update on Monday, May 27th 2025

float to byte array

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 2 Posters 3.0k Views
  • 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.
  • S Offline
    S Offline
    sandycoolxyz
    wrote on 27 Mar 2017, 13:35 last edited by
    #1

    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.

    K 1 Reply Last reply 27 Mar 2017, 20:26
    0
    • S sandycoolxyz
      27 Mar 2017, 13:35

      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.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 27 Mar 2017, 20:26 last edited by kshegunov
      #2

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

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      4

      1/2

      27 Mar 2017, 13:35

      • Login

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