Important: Please read the Qt Code of Conduct -

How can convert vector<std::complex<qint32>> to QByteArray ?

  • Is it possible to convert vector<std::complex<qint32>> to QByteArray ?

  • @isan
    I think it depends on what you mean by "convert". std::complex<qint32> is not an array of bytes, but in some shape or form its content be copied to bytes if that is what you want to do, but you must do the work yourself. I think you should read the Array-oriented access section in to see how you can access the real & imaginary parts directly, and the real() & imag() member methods.

  • Lifetime Qt Champion

    Yes but you have to do the serialization by your own.

  • @Christian-Ehrlicher can explain more about the serialization?

  • @isan
    You have to write the code to produce the QByteArray from the std::complex value ("serialization"), and the std::complex from the QByteArray ("deserialization"). There is nothing in-built to do that for you. Which is why I gave you the links to achieve that. In particular the array-like access, which you may wish to employ.

  • @JonB Thank you for your response
    I have access to real and img part, what to do after it?

    well I do this for reverse mode

    for (int i = 0; i <SHOT_SIZE; i++) {
               QByteArray sample= mDataFile->read(dataRead);
               qint32 *complex= (qint32 *);
               double Real = double(complex[1]);
               double Imag = double(complex[0]);

  • @isan
    Start with: you asked about std::complex<qint32>. Why are you now using qint16 anything [you seem to be keeping on editing your code...], and why are you doing double anything? (This may be going beyond my math knowledge!)

    Separately, you are using C-style cast (qint32 *). If you expect this to be portable across platforms, you are ignoring byte-ordering-endianness here? Who is doing the serialization (complex -> byte array -> file) in the first place (e.g. is it your code or someone else's), how (what byte order) are the integers stored?

    I have access to real and img part, what to do after it?

    std::complex<double> cmplx(Real, Imag);

  • @JonB So What difference does it make if I have real data and img or not when we complex them up in the end?
    I do this

        vector<std::complex<qint32>> cmplxData;
       QByteArray* byteData = new QByteArray(reinterpret_cast<const char*>(, cmplxIQ.size()*sizeof (std::complex<qint32>));

    and it worked

  • @isan
    In a word: you are making certain assumptions about the memory layout of std::complex<qint32>, and of a std::vector. If that works for you, it works. Whether it's robust I don't know. Nor am I sure how you are going to deserialize like this, i.e. restore from the QByteArray to a vector<std::complex<qint32>>.

    Separately, as I observed earlier, this code will not work if you exchange data between different platforms where the endianness differs.

Log in to reply