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 get uint8_t from a QByteArray
QtWS25 Last Chance

How to get uint8_t from a QByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
qbytearrayqserialport
3 Posts 2 Posters 5.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.
  • D Offline
    D Offline
    Dooham
    wrote on 16 Mar 2019, 13:36 last edited by
    #1

    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
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 16 Mar 2019, 14:00 last edited by
      #2

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

      but -128 and 128.

      Because you cast to int8_t ...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply 16 Mar 2019, 21:37
      5
      • C Christian Ehrlicher
        16 Mar 2019, 14:00

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

        but -128 and 128.

        Because you cast to int8_t ...

        D Offline
        D Offline
        Dooham
        wrote on 16 Mar 2019, 21:37 last edited by
        #3

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

        1 Reply Last reply
        0

        1/3

        16 Mar 2019, 13:36

        • Login

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