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. conversion of hex data in unsigned char array to QString.
Forum Updated to NodeBB v4.3 + New Features

conversion of hex data in unsigned char array to QString.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 6 Posters 6.6k 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.
  • T Offline
    T Offline
    Tharapathi
    wrote on 8 Mar 2019, 09:22 last edited by
    #1

    Hi, Need to convert unsigned char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6}; to QString and wanted to store in QString str.
    And it should be able to print like "13C5380380E6DC34" with qDebug()<<str;
    Please suggest.

    J K 2 Replies Last reply 8 Mar 2019, 09:55
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 8 Mar 2019, 09:41 last edited by
      #2

      You do you want to print like this ? Do you want the data like this in QString ? Since these are special characters, even if you print you will see some funny characters. After conversion just ensure that your data is fine. Tried something like this. Google hit may give many answers.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      3
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 8 Mar 2019, 09:43 last edited by
        #3

        Does this work?

        const QByteArray array(QByteArray::fromHex(buff));
        const QString string(array);
        qDebug() << string << array;
        

        Yea, like @dheerendra says, if the data is not really hex, the output will be weird characters anyway. I suggest you keep the data in QByteArray, usign QString for this is an overkill.

        (Z(:^

        1 Reply Last reply
        4
        • T Tharapathi
          8 Mar 2019, 09:22

          Hi, Need to convert unsigned char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6}; to QString and wanted to store in QString str.
          And it should be able to print like "13C5380380E6DC34" with qDebug()<<str;
          Please suggest.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 8 Mar 2019, 09:55 last edited by J.Hilk 3 Aug 2019, 09:56
          #4

          @Tharapathi
          jep the others are correct, QByteArray is the way to go.

          it has a constructor that accepts a const char array,
          https://doc.qt.io/qt-5/qbytearray.html#QByteArray-1
          and has a convenient toHex() function that prints (with QDebug) the values like you want it to
          https://doc.qt.io/qt-5/qbytearray.html#toHex

          and QString accepts a QByteArray in its constructor
          https://doc.qt.io/qt-5/qstring.html#QString-8


          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
          4
          • T Tharapathi
            8 Mar 2019, 09:22

            Hi, Need to convert unsigned char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6}; to QString and wanted to store in QString str.
            And it should be able to print like "13C5380380E6DC34" with qDebug()<<str;
            Please suggest.

            K Offline
            K Offline
            KroMignon
            wrote on 8 Mar 2019, 09:56 last edited by KroMignon 3 Aug 2019, 10:00
            #5

            @Tharapathi Try this

            char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6};
            qDebug() << QByteArray(buff, sizeof(buff)).toHex().constData();
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            A 1 Reply Last reply 8 Mar 2019, 21:08
            6
            • K KroMignon
              8 Mar 2019, 09:56

              @Tharapathi Try this

              char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6};
              qDebug() << QByteArray(buff, sizeof(buff)).toHex().constData();
              
              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 8 Mar 2019, 21:08 last edited by
              #6

              @KroMignon said in conversion of hex data in unsigned char array to QString.:

              char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6};
              qDebug() << QByteArray(buff, sizeof(buff)).toHex().constData();

              Nearly perfect. You can omit the constData() for qDebug, though.

              Qt has to stay free or it will die.

              1 Reply Last reply
              2

              1/6

              8 Mar 2019, 09:22

              • Login

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