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 can convert vector<std::complex<qint32>> to QByteArray ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 954 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by
    #1

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

    JonBJ 1 Reply Last reply
    0
    • I isan

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

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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 https://en.cppreference.com/w/cpp/numeric/complex to see how you can access the real & imaginary parts directly, and the real() & imag() member methods.

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

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

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        I 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

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

          I Offline
          I Offline
          isan
          wrote on last edited by
          #4

          @Christian-Ehrlicher can explain more about the serialization?

          JonBJ 1 Reply Last reply
          0
          • I isan

            @Christian-Ehrlicher can explain more about the serialization?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

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

            I 1 Reply Last reply
            0
            • JonBJ JonB

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

              I Offline
              I Offline
              isan
              wrote on last edited by isan
              #6

              @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 *)sample.data();
                         double Real = double(complex[1]);
                         double Imag = double(complex[0]);
                         
                     }
              
              JonBJ 1 Reply Last reply
              0
              • I isan

                @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 *)sample.data();
                           double Real = double(complex[1]);
                           double Imag = double(complex[0]);
                           
                       }
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @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);

                I 1 Reply Last reply
                0
                • JonBJ JonB

                  @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);

                  I Offline
                  I Offline
                  isan
                  wrote on last edited by isan
                  #8

                  @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*>(cmplxData.data(), cmplxIQ.size()*sizeof (std::complex<qint32>));
                  

                  and it worked

                  JonBJ 1 Reply Last reply
                  0
                  • I isan

                    @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*>(cmplxData.data(), cmplxIQ.size()*sizeof (std::complex<qint32>));
                    

                    and it worked

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    1

                    • Login

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