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. to compare QByteArray and hexadecimal value

to compare QByteArray and hexadecimal value

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 8.4k 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
    Selim
    wrote on last edited by A Former User
    #1

    Hello everyone.
    For a electronic application, i concieved an IHM which exchanges information with microcontroller.
    I use the library QSerialport to send and receive information.

    That is my reception function:

    void MainWindow::readData()
    {   
        QByteArray data = serial.readAll();
    buffer.append(data);
          
    
        if(data.at(1)==0x87)
        {
            cout<<"Détection du ping " <<endl;
          //  ui->lineEdit_PingAnswer->setText(data.toStdString());
            ui->lineEdit_Device->setText("Device STHC1 Detected");
        }
    
    }
    

    When i send the byte 0x0087, data is : "\000\207"
    data[0] : 0 '\0'
    data[1] : (hex) 87 / (hex) 87

    I want to compare if data(1) ==0x87 but it never enters in the if loop.

    Thanks to spend time on my question.

    Regards,

    jsulmJ 1 Reply Last reply
    0
    • S Selim

      Hello everyone.
      For a electronic application, i concieved an IHM which exchanges information with microcontroller.
      I use the library QSerialport to send and receive information.

      That is my reception function:

      void MainWindow::readData()
      {   
          QByteArray data = serial.readAll();
      buffer.append(data);
            
      
          if(data.at(1)==0x87)
          {
              cout<<"Détection du ping " <<endl;
            //  ui->lineEdit_PingAnswer->setText(data.toStdString());
              ui->lineEdit_Device->setText("Device STHC1 Detected");
          }
      
      }
      

      When i send the byte 0x0087, data is : "\000\207"
      data[0] : 0 '\0'
      data[1] : (hex) 87 / (hex) 87

      I want to compare if data(1) ==0x87 but it never enters in the if loop.

      Thanks to spend time on my question.

      Regards,

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Selim if(data.at(1)==(char)0x87) works for me

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        QByteArray::at returns a char. You should rather test:

        if (data.at(1) == '\x87')
        

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        jsulmJ 2 Replies Last reply
        0
        • SGaistS SGaist

          Hi,

          QByteArray::at returns a char. You should rather test:

          if (data.at(1) == '\x87')
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @SGaist warning: comparison is always false due to limited range of data type [-Wtype-limits]

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Selim
            wrote on last edited by
            #5

            Thanks for the quick answer.
            The cast (char) works :).

            I want also to create a string with 2 chars (data(0) and data(1) to fill the lineEdit_PingAnswer:
            '''
            ui->lineEdit_PingAnswer->setText(data........toStdString());
            '''

            But it's impossible to use .toStdString with data.at(1).

            I have to build the string manually ?

            jsulmJ 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              QByteArray::at returns a char. You should rather test:

              if (data.at(1) == '\x87')
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @SGaist Sorry my mistake, your version works as well

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Selim

                Thanks for the quick answer.
                The cast (char) works :).

                I want also to create a string with 2 chars (data(0) and data(1) to fill the lineEdit_PingAnswer:
                '''
                ui->lineEdit_PingAnswer->setText(data........toStdString());
                '''

                But it's impossible to use .toStdString with data.at(1).

                I have to build the string manually ?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Selim How do you want to interpret the bytes? Do you want to show '0' for the first byte in the string and "87" or "0x87" for the second?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Selim
                  wrote on last edited by
                  #8

                  I want to display the two bytes (it's the PING_ID) like:
                  in hexadecimal : 0x0087
                  Or better :
                  0000'0000'1000'0111'

                  jsulmJ 1 Reply Last reply
                  0
                  • S Selim

                    I want to display the two bytes (it's the PING_ID) like:
                    in hexadecimal : 0x0087
                    Or better :
                    0000'0000'1000'0111'

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Selim For hex you can do it like this:

                    QString str("0x0");
                    str.append(QString::number(array.at(0)));
                    str.append(QString::number((uchar)array.at(1), 16));
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Selim
                      wrote on last edited by
                      #10

                      Thanks, it's work wery well.
                      I will try to find function which translate u16 into binary form.

                      Thanks a lot for your help !

                      jsulmJ 1 Reply Last reply
                      0
                      • S Selim

                        Thanks, it's work wery well.
                        I will try to find function which translate u16 into binary form.

                        Thanks a lot for your help !

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Selim Just do:

                        str.append(QString::number(array.at(0), 2));
                        str.append(QString::number((uchar)array.at(1), 2));
                        

                        The only issue is: the first 0 byte isn't filled with 0

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Selim
                          wrote on last edited by
                          #12

                          Thanks again ^^
                          Yes, i try and display the first 0 eight times

                          1 Reply Last reply
                          0

                          • Login

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