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. How to convert float to Qbytearray in qt
Forum Updated to NodeBB v4.3 + New Features

How to convert float to Qbytearray in qt

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 313 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    Hi, I want to copy the value a float to QByteArray, I search and found these links.
    but when I convert float to QByteArray, the negative float is converted to positive float... the sign number is wrong
    How can convert float to QBytearray so that the positive/negative sign is not changed...

    Thanks in advance

    readDataComplex.append(reinterpret_cast<const char*>(&commandOneFloat), sizeof(commandOneFloat));
    readDataComplex.append(reinterpret_cast<const char*>(&commandTwoFloat), sizeof(commandTwoFloat));
    

    link 1

    JonBJ 1 Reply Last reply
    0
    • stackprogramerS stackprogramer

      Hi, I want to copy the value a float to QByteArray, I search and found these links.
      but when I convert float to QByteArray, the negative float is converted to positive float... the sign number is wrong
      How can convert float to QBytearray so that the positive/negative sign is not changed...

      Thanks in advance

      readDataComplex.append(reinterpret_cast<const char*>(&commandOneFloat), sizeof(commandOneFloat));
      readDataComplex.append(reinterpret_cast<const char*>(&commandTwoFloat), sizeof(commandTwoFloat));
      

      link 1

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @stackprogramer said in How to convert float to Qbytearray in qt:

      the sign number is wrong

      I don't know. But as per another thread recently discussed on this forum, you might try QDataStream to serialize/deserialize a float to/from a QByteArray which I presume must get it right, if you cannot resolve your direct code approach.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        anil_arise
        wrote on last edited by anil_arise
        #3

        Try it simple:

        float fNum = -78.98;
        QByteArray array = QByteArray::number(fNum);
        qDebug()<<"fNum "<<fNum <<"array"<<array;
        float floatback = array.toFloat();
        qDebug ()<<"Float "<<floatback;

        Output:
        fNum -78.98 array "-78.98"
        Float -78.98

        Details num-Qbytearray & Qbytearray-float

        JonBJ 1 Reply Last reply
        2
        • A anil_arise

          Try it simple:

          float fNum = -78.98;
          QByteArray array = QByteArray::number(fNum);
          qDebug()<<"fNum "<<fNum <<"array"<<array;
          float floatback = array.toFloat();
          qDebug ()<<"Float "<<floatback;

          Output:
          fNum -78.98 array "-78.98"
          Float -78.98

          Details num-Qbytearray & Qbytearray-float

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @anil_arise
          If he uses your QByteArray::number() approach, @stackprogramer should be aware that converts the number to a string representation in the QByteArray. And would need parsing to convert back to a number. Not what I would do for the original question/approach, but up to him.

          1 Reply Last reply
          2

          • Login

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