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. How to convert a QByteArray to std::array<unsigned char, 33UL>?
Forum Updated to NodeBB v4.3 + New Features

How to convert a QByteArray to std::array<unsigned char, 33UL>?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.1k 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    I use a library function which needs a std::array<unsigned char ,33UL> as a parameter.

    How can I convert a QByteArray into a std::array<unsigned char ,33UL>?

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

      See https://en.cppreference.com/w/cpp/container/array on how to iterate over an std::array<> (see examples). Therefore you can also append the bytes to a QByteArray.

      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
      • I Offline
        I Offline
        Infinity
        wrote on last edited by
        #3

        Would that be a possible way to do it?

        template<class T>
        T ByteArrayToStdArray(const QByteArray &byteArray)
        {
            T stdArray;
        
            //qDebug() << "std::array size" <<stdArray.size();
        
            for (unsigned int i = 0; i < stdArray.size() && i < (unsigned int) byteArray.size(); i++) {
                stdArray[i] = (unsigned char) byteArray.at(i);
            }
        
            return stdArray;
        }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Since it's a simply C array:

          std::array<unsigned char ,33UL> myArray;
          myArray[0] = 1;
          

          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
          0

          • Login

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