Conversion QByteArray to double array?



  • 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



  • 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.



  • 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?



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.