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.8k 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 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.HilkJ KroMignonK 2 Replies Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on 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
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on 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

          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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #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

            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.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #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)

            aha_1980A 1 Reply Last reply
            6
            • KroMignonK KroMignon

              @Tharapathi Try this

              char buff[]= { 0X13, 0XC5, 0X3B, 0X03, 0X80, 0XE6};
              qDebug() << QByteArray(buff, sizeof(buff)).toHex().constData();
              
              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 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

              • Login

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