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 to get a *double from a QByteArray?
Forum Update on Monday, May 27th 2025

How to get a *double from a QByteArray?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.6k 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.
  • P Offline
    P Offline
    Pedro1992
    wrote on last edited by
    #1

    Hi i'm working with a binary file that contains samples from a dongle in double format. I need those samples in *double because thats the format that my fft needs.
    But the only way that i found to take that samples off is using QByteArray or QDataStream. Can anyone tell me how to do that conversion? I will appreciate any solution.
    Here are my codes:
    First i tried with QByteArray

        QFile file("prueba.bin");
        double *datos = NULL;
        QByteArray archivo(10*sizeof(double), 0);
        archivo = file.readAll();
        memcpy(datos, &archivo , archivo.count()); //shows me error
    

    And here i tried with QDataStream

    QFile file("prueba.bin");
    double *datos = NULL;
    QVector<double> Qdatos(10);
    QDataStream mediator(&file);
    mediator >> Qdatos;
    for(int i=0; i<10 ; i++)
        {
            *(datos + i) = Qdatos[i];
        }
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      QDataStream is using an internal Format, it can not be used to read any binary data not created with QDataStream.
      You need to convert them by yourself:

      QFile file("prueba.bin");
      if (file.open(QIODevice::ReadOnly))
        return;
      const QByteArray archvio = file.readAll();
      const double*datos = reinterpret_cast<const double*>(archivo.data());
      

      And you forgot to open your QFile.

      /edit: fixed the const cast

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

      P 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        QDataStream is using an internal Format, it can not be used to read any binary data not created with QDataStream.
        You need to convert them by yourself:

        QFile file("prueba.bin");
        if (file.open(QIODevice::ReadOnly))
          return;
        const QByteArray archvio = file.readAll();
        const double*datos = reinterpret_cast<const double*>(archivo.data());
        

        And you forgot to open your QFile.

        /edit: fixed the const cast

        P Offline
        P Offline
        Pedro1992
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        Thanks for your answer but it still give me an error message.

        const char *datos = reinterpret_cast<double*>(archivo.data()); // error: reinterpret_cast from type 'const char*' to type 'double*' casts away qualifiers
          //   const char *datos = reinterpret_cast<double*>(archivo.data());
                                                                         ^
        
        aha_1980A 1 Reply Last reply
        0
        • P Pedro1992

          @Christian-Ehrlicher
          Thanks for your answer but it still give me an error message.

          const char *datos = reinterpret_cast<double*>(archivo.data()); // error: reinterpret_cast from type 'const char*' to type 'double*' casts away qualifiers
            //   const char *datos = reinterpret_cast<double*>(archivo.data());
                                                                           ^
          
          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by aha_1980
          #4

          @Pedro1992 said in How to get a *double from a QByteArray?:

          const char *datos = reinterpret_cast<double*>(archivo.data());

          That's obviously wrong.

          1. the result should be const double *
          2. cast toconst double * to fix the error

          const double *datos = reinterpret_cast<const double *>(archivo.constData());

          There are some requirements you have to assure:

          1. the number of byte in archivo.constData() must be a multiple of sizeof(double)
          2. you need to know how many double you can use after the cast (exactly the number of byte in archivo.constData() divided by sizeof(double)
          3. of course the contents in archivo.constData() must be valid double numbers, in the same endianess as your machine where you interpret them

          One more note: archivo.constData() is only valid as long as archivo is valid.

          Regards

          Qt has to stay free or it will die.

          P 1 Reply Last reply
          3
          • aha_1980A aha_1980

            @Pedro1992 said in How to get a *double from a QByteArray?:

            const char *datos = reinterpret_cast<double*>(archivo.data());

            That's obviously wrong.

            1. the result should be const double *
            2. cast toconst double * to fix the error

            const double *datos = reinterpret_cast<const double *>(archivo.constData());

            There are some requirements you have to assure:

            1. the number of byte in archivo.constData() must be a multiple of sizeof(double)
            2. you need to know how many double you can use after the cast (exactly the number of byte in archivo.constData() divided by sizeof(double)
            3. of course the contents in archivo.constData() must be valid double numbers, in the same endianess as your machine where you interpret them

            One more note: archivo.constData() is only valid as long as archivo is valid.

            Regards

            P Offline
            P Offline
            Pedro1992
            wrote on last edited by
            #5

            @aha_1980
            Thanks! it works with your solution!

            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