Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved How to get uint8_t from a QByteArray

    General and Desktop
    qbytearray qserialport
    2
    3
    3593
    Loading More Posts
    • 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.
    • Dooham
      Dooham last edited by

      Hello,
      I have the following trouble: I have a QSerialPort that read the messages from a Pixhawk (a component that transmit messages in a Hexadecimal base, with values from 0 to 255). When I read the messages I store this datas in a QByteArray, that has the following apparence:
      0_1552741930079_Problema2Foto1.PNG

      At this point, I passed this QByteArray to other one with the command toHex and I get this QByteArray:
      0_1552741984416_Problema2Foto2.PNG

      But now I want to access to the byte of the original messages thought the last one QByteArray. I know that the information in the message is store in every two characters, so every byte is a number that has some kind of meaning that I can translate with a library called MavLink.
      However I dont know how to access to one byte from the last QByteArray, i can access to the chars and the I can append this char to a string and then get to value in a int though toInt. But with this method I have two troubles: its really heavy and, overall, in some time I will need to construct variables uint16_t or uint64_t and I dont know what to do to get it.

      
      int pasarNumeroaINT8 (QByteArray as, int i1){
          char prim=as.at(i1), sec=as.at(i1+1);
          QString byte = ""; byte.append(prim); byte.append(sec);
          bool ok;
         int byteInt = byte.toInt(&ok, 16);
      
         return byteInt;
      
      }
      

      I tried other method, and it was make it with the original QByteArray and a static cast, but I dont get values between 0 and 255, but -128 and 128.

      data = serial->readAll();
      
          dataAux = data.toHex();
      
           qDebug()<<static_cast<int8_t>(data[0]);
      

      Anyone can help me? Thanks you.

      Sorry for my English.

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        @Dooham said in How to get uint8_t from a QByteArray:

        but -128 and 128.

        Because you cast to int8_t ...

        Qt has to stay free or it will die.

        Dooham 1 Reply Last reply Reply Quote 5
        • Dooham
          Dooham @Christian Ehrlicher last edited by

          @Christian-Ehrlicher Thanks, that was exactly my problem.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post