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 Updated to NodeBB v4.3 + New Features

float to byte array

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 2 Posters 3.0k Views 2 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.
  • S Offline
    S Offline
    sandycoolxyz
    wrote on 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.

    kshegunovK 1 Reply Last reply
    0
    • S sandycoolxyz

      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.

      kshegunovK Away
      kshegunovK Away
      kshegunov
      Moderators
      wrote on 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

      • Login

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