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 QByteArray to unsigned char
Forum Updated to NodeBB v4.3 + New Features

Converting QByteArray to unsigned char

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 31.5k 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.
  • James SprinksJ Offline
    James SprinksJ Offline
    James Sprinks
    wrote on last edited by
    #1

    Hi all

    I have a populated QByteArray - filled with hexadecimal numbers (0x01, 0x41 etc.).

    I need to convert this to a unsigned char array of the same size. Any Ideas?

        QByteArray data;
        data[0] = 0x41;
        data[1] = 0x12;
    
        //Append ff codes to datastream (representing no change)
        for (int i = 0; i < 35; i++)
        {
            data.append(0xFF);
        }
    
        int count = data.size();
        unsigned char hex[count];
    

    How do I populate the unsigned char hex with the contents of the QByteArray data?

    Thanks

    James

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      unsigned char hex[count]; is invalid in C++, it only compiles if your compiler treats it as C, as far as I know only gcc does that by default

      const std::size_t count = data.size();
      unsigned char* hex =new unsigned char[count];
      std::memcpy(hex,data.constData(),count);
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      James SprinksJ 1 Reply Last reply
      3
      • VRoninV VRonin

        unsigned char hex[count]; is invalid in C++, it only compiles if your compiler treats it as C, as far as I know only gcc does that by default

        const std::size_t count = data.size();
        unsigned char* hex =new unsigned char[count];
        std::memcpy(hex,data.constData(),count);
        
        James SprinksJ Offline
        James SprinksJ Offline
        James Sprinks
        wrote on last edited by
        #3

        @VRonin I get this error with that code:

        C:\PMPS\PMPSv1\pmps_f.cpp:1605: error: reinterpret_cast from type 'const char*' to type 'unsigned char*' casts away qualifiers
        memcpy(hex,reinterpret_cast<unsigned char*>(data.constData()),count);
        ^

        VRoninV 1 Reply Last reply
        0
        • James SprinksJ James Sprinks

          @VRonin I get this error with that code:

          C:\PMPS\PMPSv1\pmps_f.cpp:1605: error: reinterpret_cast from type 'const char*' to type 'unsigned char*' casts away qualifiers
          memcpy(hex,reinterpret_cast<unsigned char*>(data.constData()),count);
          ^

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @James-Sprinks I ninja'd in my correction. it was easy btw

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          James SprinksJ 1 Reply Last reply
          1
          • VRoninV VRonin

            @James-Sprinks I ninja'd in my correction. it was easy btw

            James SprinksJ Offline
            James SprinksJ Offline
            James Sprinks
            wrote on last edited by
            #5

            @VRonin So what is the correction?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @VRonin said in Converting QByteArray to unsigned char:

              std::memcpy(hex,data.constData(),count);

              std::memcpy(hex,data.constData(),count); as written above

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              James SprinksJ 1 Reply Last reply
              2
              • VRoninV VRonin

                @VRonin said in Converting QByteArray to unsigned char:

                std::memcpy(hex,data.constData(),count);

                std::memcpy(hex,data.constData(),count); as written above

                James SprinksJ Offline
                James SprinksJ Offline
                James Sprinks
                wrote on last edited by
                #7

                @VRonin Brilliant. Thank you!

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  On a side note, you could also use reinterpret_cast<unsigned char*>(data.data()) if you don't need to copy it to a separate array

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  3
                  • aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Sorry for hijacking, but what do you guys think of QTBUG-64746?

                    It seems very common to use QByteArray as a raw byte container and sometimes you need unsigned char instead char.

                    Qt has to stay free or it will die.

                    J.HilkJ 1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      Sorry for hijacking, but what do you guys think of QTBUG-64746?

                      It seems very common to use QByteArray as a raw byte container and sometimes you need unsigned char instead char.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #10

                      @aha_1980 I wouldn't be averse to it

                      I have a case were I check specific bytes for values first, before managing the whole byte array and some checks are higher than 0x7F.
                      The comparing operator ==(0xAA) works fine, but is always taged with a warning. Which tickles my OCD.

                      To your point made in the Bugreport-ticket.

                      In my current case, a QtSerialBus communication, I combine 2 chars to a short via QDatatStream.

                      {//section from my class
                      ...
                           QDataStream out(m_data);
                           quint8 countData;
                           QVector<quint16> values;
                           out >> countData;
                           countData = countData/2;
                      
                           quint16 value;
                           for(int i(0); i <countData; i++){
                                 out >> value;
                                 if(out.status() == QDataStream::Ok)
                                       values.append(value);
                                 }
                            return values;    
                      }
                      

                      I did this because I didn't want to use all the reinterpret_cast's. Tried it first, became unreadable pretty quickly x)


                      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.

                      aha_1980A 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @aha_1980 I wouldn't be averse to it

                        I have a case were I check specific bytes for values first, before managing the whole byte array and some checks are higher than 0x7F.
                        The comparing operator ==(0xAA) works fine, but is always taged with a warning. Which tickles my OCD.

                        To your point made in the Bugreport-ticket.

                        In my current case, a QtSerialBus communication, I combine 2 chars to a short via QDatatStream.

                        {//section from my class
                        ...
                             QDataStream out(m_data);
                             quint8 countData;
                             QVector<quint16> values;
                             out >> countData;
                             countData = countData/2;
                        
                             quint16 value;
                             for(int i(0); i <countData; i++){
                                   out >> value;
                                   if(out.status() == QDataStream::Ok)
                                         values.append(value);
                                   }
                              return values;    
                        }
                        

                        I did this because I didn't want to use all the reinterpret_cast's. Tried it first, became unreadable pretty quickly x)

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @J.Hilk said in Converting QByteArray to unsigned char:

                        I did this because I didn't want to use all the reinterpret_cast's. Tried it first, became unreadable pretty quickly x)

                        That's my motivation also. Of course QDataStream is the correct solution, but that's still a lot of code lines for a simple problem. And sometimes you just want to create a QByteArray with some values in it, which should be a one-liner IMHO.

                        Qt has to stay free or it will die.

                        J.HilkJ 1 Reply Last reply
                        0
                        • aha_1980A aha_1980

                          @J.Hilk said in Converting QByteArray to unsigned char:

                          I did this because I didn't want to use all the reinterpret_cast's. Tried it first, became unreadable pretty quickly x)

                          That's my motivation also. Of course QDataStream is the correct solution, but that's still a lot of code lines for a simple problem. And sometimes you just want to create a QByteArray with some values in it, which should be a one-liner IMHO.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @aha_1980 said in Converting QByteArray to unsigned char:

                          And sometimes you just want to create a QByteArray with some values in it, which should be a one-liner IMHO.

                          verry true,
                          I'm missing a simple constructor/initalizer list functionality

                          for example:

                          //I wish this was possible 
                          QByteArray ba{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
                          

                          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
                          • G Offline
                            G Offline
                            guitarpicva
                            wrote on last edited by guitarpicva
                            #13

                            Try the macro QByteArrayLiteral

                            https://doc.qt.io/qt-5/qbytearray.html#QByteArrayLiteral

                            QByteArray b = QByteArrayLiteral("\x12\x00\xa4\x42\x51\x00\x00\x99");

                            1 Reply Last reply
                            3

                            • Login

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