简体   繁体   English

QDatastream中的qt二进制读取错误

[英]Qt binary reading error in qDatastream

I am reading a binary file that is a produced by a sensor. 我正在读一个由传感器产生的二进制文件。 I am having problem in reading float with different precision (32 or 64). 我在以不同的精度(32或64)读取浮点数时遇到问题。 I can read them in MATLAB (64 bit version) but Qt (32 bit version on windows) is giving wrong values. 我可以在MATLAB(64位版本)中读取它们,但Qt(Windows上的32位版本)给出了错误的值。 I can read till dtmth (please ref structure below) . 我可以读到dtmth (请参考下面的结构)。 After it I am getting a value Inf for baseline . 在它之后,我获得baselineInf This value is 0 in fact. 实际上该值为0 As you can see, I changed MSB (LittleEndian). 如你所见,我改变了MSB(LittleEndian)。 If I keep BigEndian, I get 0 for baseline but others values are wrong then. 如果我保留BigEndian,我的基线为0 ,但其他值则错误。 My desktop is 64 bit. 我的桌面是64位。

I have checked number of bytes and they are correct. 我检查了字节数,它们是正确的。 I think the problem is machine precision. 我认为问题是机器精度。

QDataStream in(&file);

           in.setByteOrder(QDataStream::LittleEndian);
           params p;

           in >> p.filetype;               
           in >> p.projectid;
           in >> p.datamin;               
           in >> p.dtyear;
           in >> p.dtmth;              
           in >> p.baseline;
           in >> p.startfrequ;

Where p is a structure defined as: 其中p是定义为的结构:

    struct params
    {
        quint8 filetype;   
        quint16 projectid;
        double datamin;
        quint16 dtyear;
        quint8 dtmth;    
        float baseline;
        double startfrequ;

    };

I can read them in MATLAB. 我可以在MATLAB中阅读它们。 My matlab is 64 bit version where I read data types as following: 我的matlab是64位版本,我在其中读取数据类型如下:

MATLAB:
        uint8 filetype;   
        uint16 projectid;
        float64 datamin;
        uint16 dtyear;
        uint8 dtmth;    
        float32 baseline;
        float64 startfrequ;

Let me know if I missed any details. 如果我错过任何细节,请告诉我。

EDIT: 编辑:

Reading file: 阅读文件:

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
               tr("Raw Files (*.msr);;All files (*.*)"));

           if (!fileName.isEmpty()) {
               qDebug("Attempting to open file..");
               QFile file(fileName);
               if (!file.open(QIODevice::ReadOnly)) {
                   QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
                   return;
               }

               QDataStream in(&file);

Thanks a lot in advance. 非常感谢提前。

Which version of Qt are you using? 您使用的是哪个版本的Qt? If the version is superior to Qt 4.6, then the default precision is 64 bit, which means Qt will try to read your float 32 as a float 64 . 如果版本优于Qt 4.6,则默认精度为64位, 这意味着Qt将尝试将浮点数32作为浮点数64读取 You need to manually set the precision with in.setFloatingPointPrecision ( QDataStream::SinglePrecision); 您需要使用in.setFloatingPointPrecision ( QDataStream::SinglePrecision);手动设置精度in.setFloatingPointPrecision ( QDataStream::SinglePrecision);

       in >> p.filetype;               
       in >> p.projectid;
       in >> p.datamin;               
       in >> p.dtyear;
       in >> p.dtmth;    
       in.setFloatingPointPrecision(QDataStream::SinglePrecision);
       in >> p.baseline;
       in.setFloatingPointPrecision(QDataStream::DoublePrecision);
       in >> p.startfrequ;

From your comment it seems this was the issue. 从您的评论看,这似乎是问题。 Indeed, if you set to single precision, and you try to read p.datamin or p.startfrequ (64 bits), then the data stream will read them as 32 bits floats. 实际上,如果设置为单精度,并且尝试读取p.dataminp.startfrequ (64位),则数据流将读取它们为32位浮点数。 and not only p.datamin will be incorrect but all values after it. 并且不仅p.datamin不正确,而且所有值都在它之后。

For a start, to check that my suggestion worked use after the last line 首先,检查我的建议是否在最后一行之后使用

      if(in.status() == QDataStream::ReadCorruptData){
            qDebug() << "still doesnt work";
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM