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. [solved] Convert a char array into qint16 array with qFromBigEndian
Forum Update on Monday, May 27th 2025

[solved] Convert a char array into qint16 array with qFromBigEndian

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 735 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.
  • T Offline
    T Offline
    Thinium
    wrote on 12 Jun 2020, 21:45 last edited by Thinium
    #1

    In my program I receive an array of const char *data, It's BigEndian type so I would like to use the function qFromBigEndian<qint16> to convert the array into a QVector of qint16. But it crashed. I assume that I can't just convert it directly from the char array into a QVector, what is the correct way of converting the char array into a qint16 array? Here is the code snippet:

    void TestFunction::receiveData(const char *data, int channelCount)
    {
        // channelCount is the number of qint16 the data contains
        QVector<qint16> destination;
    
        // the following line crashes: what is the correct data type for destination?
        qFromBigEndian<qint16>(ptr, channelCount, &destination)
    }
    

    Thanks!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on 13 Jun 2020, 03:41 last edited by Bonnie
      #2

      You need to first allocate the memory by making the vector non-empty and having the same byte size as you want to read from the const char *data, and then pass the allocated memory address, not the vector variable's address, to the function.

      QVector<qint16> destination(channelCount);
      qFromBigEndian<qint16>(data, channelCount, destination.data());
      

      And you need to be sure that channelCount = bytesizeofdata / 2.

      1 Reply Last reply
      4
      • T Offline
        T Offline
        Thinium
        wrote on 13 Jun 2020, 08:50 last edited by
        #3

        @Bonnie said in Convert a char array into qint16 array with qFromBigEndian:

        And you need to be sure that channelCount = bytesizeofdata / 2.

        Hi @Bonnie, thanks a lot for your help! I still can't get the second argument right. In my case, the data array is of length 4096 byte, but I only want to copy the first 64 numbers out of the array as qint16 into my QVector (meaning the destination array would be pre-allocated with size 64). Is that possible with this function at all? Or I need to copy the whole data source into the destination?

        M B 2 Replies Last reply 13 Jun 2020, 09:02
        0
        • T Thinium
          13 Jun 2020, 08:50

          @Bonnie said in Convert a char array into qint16 array with qFromBigEndian:

          And you need to be sure that channelCount = bytesizeofdata / 2.

          Hi @Bonnie, thanks a lot for your help! I still can't get the second argument right. In my case, the data array is of length 4096 byte, but I only want to copy the first 64 numbers out of the array as qint16 into my QVector (meaning the destination array would be pre-allocated with size 64). Is that possible with this function at all? Or I need to copy the whole data source into the destination?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 13 Jun 2020, 09:02 last edited by mrjj
          #4

          @Thinium
          Hi
          Maybe i misunderstood something but cant you just do
          qFromBigEndian<qint16>(data, 64, destination.data());

          if you dont want all of channelCount ?

          T 1 Reply Last reply 15 Jun 2020, 19:36
          2
          • T Thinium
            13 Jun 2020, 08:50

            @Bonnie said in Convert a char array into qint16 array with qFromBigEndian:

            And you need to be sure that channelCount = bytesizeofdata / 2.

            Hi @Bonnie, thanks a lot for your help! I still can't get the second argument right. In my case, the data array is of length 4096 byte, but I only want to copy the first 64 numbers out of the array as qint16 into my QVector (meaning the destination array would be pre-allocated with size 64). Is that possible with this function at all? Or I need to copy the whole data source into the destination?

            B Offline
            B Offline
            Bonnie
            wrote on 13 Jun 2020, 09:14 last edited by Bonnie
            #5

            @Thinium
            Just as @mrjj said, if you only need 64 numbers, then

            int count = qMin(64, channelCount);  //just in case of channelCount is not big enough
            QVector<qint16> destination(count);
            qFromBigEndian<qint16>(data, count, destination.data());
            
            1 Reply Last reply
            3
            • T Offline
              T Offline
              Thinium
              wrote on 13 Jun 2020, 09:38 last edited by
              #6

              Thanks to @mrjj and @Bonnie ! it works. My problem actually was that the data source is 32 bit but not 16 bit. So I need to use qint32 instead. All good now :)

              1 Reply Last reply
              1
              • M mrjj
                13 Jun 2020, 09:02

                @Thinium
                Hi
                Maybe i misunderstood something but cant you just do
                qFromBigEndian<qint16>(data, 64, destination.data());

                if you dont want all of channelCount ?

                T Offline
                T Offline
                Thinium
                wrote on 15 Jun 2020, 19:36 last edited by
                #7

                @mrjj for some reason I need to use std::vector<qint32> instead of QVector<qint32> as the destination, but I notice that if I use:

                std::vector<qint32> destination;
                destination.reserve(channelCount);
                qFromBigEndian<qint32>(data, 64, destination.data());
                

                The destination doesn't get filled with anything . Does that mean the destination could only be a QVector?

                M B 2 Replies Last reply 15 Jun 2020, 19:48
                0
                • T Thinium
                  15 Jun 2020, 19:36

                  @mrjj for some reason I need to use std::vector<qint32> instead of QVector<qint32> as the destination, but I notice that if I use:

                  std::vector<qint32> destination;
                  destination.reserve(channelCount);
                  qFromBigEndian<qint32>(data, 64, destination.data());
                  

                  The destination doesn't get filled with anything . Does that mean the destination could only be a QVector?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 15 Jun 2020, 19:48 last edited by
                  #8

                  Hi
                  Well its
                  template <typename T> void qFromBigEndian(const void *src, qsizetype count, void *dest)

                  so std::vector should also work as far as i can see.

                  1 Reply Last reply
                  2
                  • T Thinium
                    15 Jun 2020, 19:36

                    @mrjj for some reason I need to use std::vector<qint32> instead of QVector<qint32> as the destination, but I notice that if I use:

                    std::vector<qint32> destination;
                    destination.reserve(channelCount);
                    qFromBigEndian<qint32>(data, 64, destination.data());
                    

                    The destination doesn't get filled with anything . Does that mean the destination could only be a QVector?

                    B Offline
                    B Offline
                    Bonnie
                    wrote on 15 Jun 2020, 23:33 last edited by Bonnie
                    #9

                    @Thinium
                    Of course std::vector is also usable.
                    The problem is reserve() only allocate the memory, but the size() is still 0.
                    The qFromBigEndian function only fills the memory, it won't increase the vector size.
                    Also with an empty vector, the result of data() may be invalid.
                    So, either use

                    std::vector<qint32> destination(channelCount);
                    

                    or

                    std::vector<qint32> destination;
                    destination.resize(channelCount);
                    
                    1 Reply Last reply
                    4

                    1/9

                    12 Jun 2020, 21:45

                    • Login

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