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. QByteArray of char* to QString

QByteArray of char* to QString

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 567 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.
  • M Offline
    M Offline
    MScottM
    wrote on last edited by
    #1

    Here is my code:

    //snip
    if(busOn){
            long id;
            QString l;            
            QString canMessage;           
            unsigned char data[8];
            unsigned int dlc, flags;
            unsigned long timestamp;
    
            canStatus status = canReadWait(canHandle, &id, data, &dlc, &flags, &timestamp, 100);
    
             QByteArray msgData = QByteArray((char*)data, 8);
            
            if (status == canOK){
                QTextStream(&l) << QString("%1").arg(id, 8, 16, QLatin1Char( '0' )) << msgData;
            }
            m_ui->textEditCanIn->append(l);
                if (canMessage != "") {
                    qDebug() << canMessage;
                    //emit canMessageOut(canMessage);
                }
    

    The debug statement at the end prints out this:
    "18ff0301\u0004\u0000\u0000\u0000\u0000\u0000\u0000\u0000"

    The first part of the message is fine (the message ID), and the second part contains the data I expect to see (struggled for a bit to get there), but I need to put it into a QString format like this:
    "18ff0301[0x]0400000000000000"

    so I can send it to another function for processing (the emit canMessageOut). I've been struggling with how to format the message and having no luck converting a QByteArray of char* to a QString.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      If you want to show the content of the byte array as hex values so the user can read it, then you should use toHex()

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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        MScottM
        wrote on last edited by
        #3

        Hi @Christian-Ehrlicher ,

        I tried this:

        QByteArray msgData = QByteArray(data).toHex();
        

        (had to change from unsiged char to char)

        but then the debug statement prints nothing for the data (only the ID):

        "18ff0301"

        Christian EhrlicherC 1 Reply Last reply
        0
        • M MScottM

          Hi @Christian-Ehrlicher ,

          I tried this:

          QByteArray msgData = QByteArray(data).toHex();
          

          (had to change from unsiged char to char)

          but then the debug statement prints nothing for the data (only the ID):

          "18ff0301"

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MScottM said in QByteArray of char* to QString:

          but then the debug statement prints nothing for the data

          Because now you don't pass the size of your char* array to the QByteArray ctor.

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

          1 Reply Last reply
          2
          • M Offline
            M Offline
            MScottM
            wrote on last edited by
            #5

            Dang, that was it. I swear I tried that yesterday, but I probably had it wrong as I was tired and throwing code around trying to make it work.

            Thanks once again for the help!

            ? 1 Reply Last reply
            2
            • M MScottM

              Dang, that was it. I swear I tried that yesterday, but I probably had it wrong as I was tired and throwing code around trying to make it work.

              Thanks once again for the help!

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6
              This post is deleted!
              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