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. Float to Qbytearray in hex format
Forum Updated to NodeBB v4.3 + New Features

Float to Qbytearray in hex format

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 3.1k 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.
  • S Offline
    S Offline
    sandycoolxyz
    wrote on last edited by
    #1

    Hi,
    I am using qt 4.8.5.
    I would like to append 4byte float value to qbytearray.
    eg. float f = 32.6 (0x42026666)
    if qbytearray is 0x6537, i would like to append the float value in hex as well.
    i.e result = 0x653742026666

    i tried this but i didnt get the result that i expected.

    bytes.append(reinterpret_cast<const char*>(&f), sizeof(f));
    

    thanks in advance.

    K m.sueM 2 Replies Last reply
    0
    • S sandycoolxyz

      Hi,
      I am using qt 4.8.5.
      I would like to append 4byte float value to qbytearray.
      eg. float f = 32.6 (0x42026666)
      if qbytearray is 0x6537, i would like to append the float value in hex as well.
      i.e result = 0x653742026666

      i tried this but i didnt get the result that i expected.

      bytes.append(reinterpret_cast<const char*>(&f), sizeof(f));
      

      thanks in advance.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @sandycoolxyz said in Float to Qbytearray in hex format:

      if qbytearray is 0x6537, i would like to append the float value in hex as well.

      Not exactly clear what you mean here.
      Is this already stored as different characters '0', 'x', '6', '5', '3', '7' or is it a two byte bytearray of 0x65, 0x37?

      Note, "0x" is typically printed as addition for human interpretation. It is not typically expected to be part of QByteArray.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S sandycoolxyz

        Hi,
        I am using qt 4.8.5.
        I would like to append 4byte float value to qbytearray.
        eg. float f = 32.6 (0x42026666)
        if qbytearray is 0x6537, i would like to append the float value in hex as well.
        i.e result = 0x653742026666

        i tried this but i didnt get the result that i expected.

        bytes.append(reinterpret_cast<const char*>(&f), sizeof(f));
        

        thanks in advance.

        m.sueM Offline
        m.sueM Offline
        m.sue
        wrote on last edited by m.sue
        #3

        Hi @sandycoolxyz

        did you try it the same as with the integers already:

        union (
        int iValue;
        float fValue;
        };
        bytes.append(QByteArray::number(iValue,16)).rightJustified(8,'0');
        

        Of course, you should consider to use a more C++-like approach that works with all standard data types:

        QByteArray data;
        QDataStream io(&data,QIODevice::WriteOnly);
        io << fValue;
        

        -Michael.

        1 Reply Last reply
        3
        • S Offline
          S Offline
          sandycoolxyz
          wrote on last edited by
          #4

          @m-sue
          I used the union method. Works fine with the positive numbers.
          But if its negative value following gets appended ffffffffc201999a instead of c201999a

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @m.sue said in Float to Qbytearray in hex format:

            int iValue;

            use quint32 iValue; instead

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            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