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 QByteArray to double array?
QtWS25 Last Chance

Conversion QByteArray to double array?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.3k 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.
  • S Offline
    S Offline
    stackprogramer
    wrote on 14 Dec 2016, 06:14 last edited by A Former User
    #1

    hi, i had a QByteArray with size 1000.
    now i need my data in Double Array or int Araay when i uses

    std::copy(int array,int array+N,QByteArray buffer)
    

    it retruns errors:

    /usr/include/c++/5/bits/stl_algobase.h:392: error: no type named 'value_type' in 'struct std::iterator_traits<QByteArray>'
           typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
                                                             ^
    /usr/include/c++/5/bits/stl_algobase.h:397: error: no type named 'value_type' in 'struct std::iterator_traits<QByteArray>'
             && __are_same<_ValueTypeI, _ValueTypeO>::__value);
             ^
    

    how i can convert QbyteArray to int array
    i don't want use buffer.at[i] and forl loop structure
    more info http://stackoverflow.com/questions/1908440/c-typecast-array
    thank in advance

    1 Reply Last reply
    0
    • M Offline
      M Offline
      m.sue
      wrote on 14 Dec 2016, 07:44 last edited by m.sue
      #2

      Hi,
      let's say the QByteArray contains the 100 integers in binary form. You can use memcpy for this. The following should do it:

      int array[100];
      QByteArray numbers(100*sizeof(int),0);
      memcpy(array,numbers.data(),numbers.count());
      

      Take care: on the usual machines, you will have to set QByteArray to "little endian" while it gets filled.
      -Michael.

      1 Reply Last reply
      2
      • V Offline
        V Offline
        VRonin
        wrote on 14 Dec 2016, 07:56 last edited by
        #3

        It all depends what the byte array contains, if it contains the binary representation of the int array then you can use memcopy or, if you do not need to make changes, you can just use a cast const int* array=reinterpret_cast<const int*>(numbers.constData()); if you want to convert each char in the QByteArray in an int instead then you have to use a loop (or a std::copy which is the same).

        I strongly suspect however you are doing something wrong. could you post how you write to that QByteArray?

        "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
        2
        • S Offline
          S Offline
          stackprogramer
          wrote on 14 Dec 2016, 19:21 last edited by
          #4

          finally i define my int8 array because my data is in any byte, i worried about speed conversion but speed is good for large frame UDP socket
          snippet code:

           socket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
             StringBuffer=buffer.toHex();
          __int8_t array[1000];
             memcpy(array,buffer.data(),100);
          
          

          thanks from all best regards stackprogramer

          1 Reply Last reply
          0

          3/4

          14 Dec 2016, 07:56

          • Login

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