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. Converting string to hex before inserting into QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Converting string to hex before inserting into QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 488 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.
  • L Offline
    L Offline
    lukutis222
    wrote on 14 Sept 2022, 06:26 last edited by
    #1

    Hello. I have been working with my serial terminal program. When I connect to external device, I can read incoming data and print it out on my console without any issues.
    0834049b-d6d6-431f-abf2-eeddbd742916-image.png

    I would like to improve my console and allow user to convert all incoming data to hex and print it out to the console. I have made a checkbox and before I insert data to the QTextEdit I check if the checkbox is checked as following:

    void Widget::readData()
    {
        QByteArray data = serial_local->serial_connection.readAll();
        QString DataAsString = QString(data);
    //handle data in hex format
        if(ui->hex_checkbox->isChecked())
        {
                QByteArray data_to_transmit = QByteArray::fromHex(data);
                qDebug() << data_to_transmit;
                ui->Console_read->insertHtml(data_to_transmit);
         }
    //handle data in string format
       else
       {
                DataAsString.replace("\r", "\n");
                ui->Console_read->insertPlainText(DataAsString);
       }
    
    }
    

    As you can see from above, I have tried the following to covert data to hex as following:

        if(ui->hex_checkbox->isChecked()){
            QByteArray data_to_transmit = QByteArray::fromHex(data);
            //QString res = DataAsString.toUtf8().toHex();
            qDebug() << data_to_transmit;
            ui->Console_read->insertHtml(data_to_transmit);
        }
    

    I can see on the application output:

    "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
    "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
    "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
    "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
    

    but in my console I see:
    ccb0c744-1dd0-4641-92ca-47b0f1c3d15e-image.png

    It is quite strange because the external device that I am connected to is sending me the following data:

    while(1){
            printf("hello message1 \n");
            printf("hello message2 \n");
            delay(1000);
            printf("new message1 \r");
            printf("new message2 \r");
            delay(1000);
    }
    

    So I am expecting to receive the following data:

    68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 31 0A // hello message1
    68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 32 0A //hello message2
    6E 65 77 20 6D 65 73 73 61 67 65 31 0A // new message1
    6E 65 77 20 6D 65 73 73 61 67 65 32 0A // new message1
    
    J 1 Reply Last reply 14 Sept 2022, 06:29
    0
    • L lukutis222
      14 Sept 2022, 06:26

      Hello. I have been working with my serial terminal program. When I connect to external device, I can read incoming data and print it out on my console without any issues.
      0834049b-d6d6-431f-abf2-eeddbd742916-image.png

      I would like to improve my console and allow user to convert all incoming data to hex and print it out to the console. I have made a checkbox and before I insert data to the QTextEdit I check if the checkbox is checked as following:

      void Widget::readData()
      {
          QByteArray data = serial_local->serial_connection.readAll();
          QString DataAsString = QString(data);
      //handle data in hex format
          if(ui->hex_checkbox->isChecked())
          {
                  QByteArray data_to_transmit = QByteArray::fromHex(data);
                  qDebug() << data_to_transmit;
                  ui->Console_read->insertHtml(data_to_transmit);
           }
      //handle data in string format
         else
         {
                  DataAsString.replace("\r", "\n");
                  ui->Console_read->insertPlainText(DataAsString);
         }
      
      }
      

      As you can see from above, I have tried the following to covert data to hex as following:

          if(ui->hex_checkbox->isChecked()){
              QByteArray data_to_transmit = QByteArray::fromHex(data);
              //QString res = DataAsString.toUtf8().toHex();
              qDebug() << data_to_transmit;
              ui->Console_read->insertHtml(data_to_transmit);
          }
      

      I can see on the application output:

      "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
      "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
      "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
      "\xEE\xAE\x1E\xEA\xE2\xEE\xAE\x1E\xEA\xE2"
      

      but in my console I see:
      ccb0c744-1dd0-4641-92ca-47b0f1c3d15e-image.png

      It is quite strange because the external device that I am connected to is sending me the following data:

      while(1){
              printf("hello message1 \n");
              printf("hello message2 \n");
              delay(1000);
              printf("new message1 \r");
              printf("new message2 \r");
              delay(1000);
      }
      

      So I am expecting to receive the following data:

      68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 31 0A // hello message1
      68 65 6C 6C 6F 20 6D 65 73 73 61 67 65 32 0A //hello message2
      6E 65 77 20 6D 65 73 73 61 67 65 31 0A // new message1
      6E 65 77 20 6D 65 73 73 61 67 65 32 0A // new message1
      
      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 14 Sept 2022, 06:29 last edited by
      #2

      @lukutis222 QByteArray::fromHex != toHex :D

      https://doc.qt.io/qt-6/qbytearray.html#toHex


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      L 1 Reply Last reply 14 Sept 2022, 06:52
      2
      • J J.Hilk
        14 Sept 2022, 06:29

        @lukutis222 QByteArray::fromHex != toHex :D

        https://doc.qt.io/qt-6/qbytearray.html#toHex

        L Offline
        L Offline
        lukutis222
        wrote on 14 Sept 2022, 06:52 last edited by lukutis222
        #3

        @J-Hilk
        Thanks for response. I am very confused. Is this an error in QT documentation?

        It talks about toHex but then in the example it uses fromHex

        24cd818e-63c4-4ce9-8326-c002f7e7a6ba-image.png

        Regardless of this, I think I have managed to get it to work:

            QByteArray data = serial_local->serial_connection.readAll();
            QString DataAsString = QString(data);
        
            if(ui->hex_checkbox->isChecked()){
                QByteArray data_to_transmit = data.toHex();
                //QString res = DataAsString.toUtf8().toHex();
                qDebug() << data_to_transmit;
                ui->Console_read->insertHtml(data_to_transmit);
            }
        

        Thank you

        J 1 Reply Last reply 14 Sept 2022, 06:58
        1
        • L lukutis222
          14 Sept 2022, 06:52

          @J-Hilk
          Thanks for response. I am very confused. Is this an error in QT documentation?

          It talks about toHex but then in the example it uses fromHex

          24cd818e-63c4-4ce9-8326-c002f7e7a6ba-image.png

          Regardless of this, I think I have managed to get it to work:

              QByteArray data = serial_local->serial_connection.readAll();
              QString DataAsString = QString(data);
          
              if(ui->hex_checkbox->isChecked()){
                  QByteArray data_to_transmit = data.toHex();
                  //QString res = DataAsString.toUtf8().toHex();
                  qDebug() << data_to_transmit;
                  ui->Console_read->insertHtml(data_to_transmit);
              }
          

          Thank you

          J Online
          J Online
          J.Hilk
          Moderators
          wrote on 14 Sept 2022, 06:58 last edited by
          #4

          @lukutis222 said in Converting string to hex before inserting into QTextEdit:

          Is this an error in QT documentation?
          It talks about toHex but then in the example it uses fromHex

          no, not really. The example uses fromHex in the first line to fill the data of the QByteArray, which is internally a char array.

          QByteArray does currently not support initialiser list construction, so the documentation uses the fromHex(string) static function to create and fill the QByteArray in one line


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1

          1/4

          14 Sept 2022, 06:26

          • Login

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