将dat文件转换为数字

时间:2015-08-06 10:14:03

标签: c++ binary

我有一个dat文件,我只想从中提取数字。 我用c ++编写了一个代码,但我不知道如何将char数组转换为可读的数字。

在第24页的下面的链接中,您可以看到dat的手册。文件: http://www.psi.ch/drs/DocumentationEN/manual_rev50.pdf (我只需要时间和电压数字)。

这是我的尝试:

 app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            ExpireTimeSpan = TimeSpan.FromMinutes(30),
            SlidingExpiration = true,
            LoginPath = new PathString("/signin"),
            CookieName = ".Cookies",
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });
正如您可能看到的那样,输出数据文件如下所示:

•«&GT;

[¬&GT;

‰〜

依旧......

我该怎么办? 谢谢!

更新

这是我的新代码:

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main()
{

ifstream InpData;
char filename[256];
cout<<"Enter file name:"<<endl;
cin>>filename;
InpData.open(filename,ios_base::binary);
while(InpData==0)
{
    cout<<"You entered wrong address, enter again file name:"<<endl;
    cin>>filename;
    InpData.open(filename,std::ifstream::binary);
}
InpData.seekg (0, InpData.end);
int length = InpData.tellg();
InpData.seekg (0, InpData.beg);

char *buffer=new char[length];

ofstream Outpdata;
Outpdata.open("results.txt");
    std::cout << "Reading " << length << " characters... ";
    InpData.read(buffer,length);
    if (InpData)
    {
         std::cout << "all characters read successfully."<<endl;
        int c=0;
         for(int i=0; i<1024; i++)
         {
             for(int j=((i*4)+12); j<((i*4)+12)+4; j++)
             {
                Outpdata<<buffer[j];

             }
             Outpdata<<endl;
         }
    }

    else
      std::cout << "error: only " << InpData.gcount() << " could be read";
    InpData.close();
    Outpdata.close();

    delete[] buffer;


  }


}

我仍然只得到零......

read_binary.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
using namespace std;

    int main()
{ 

    ifstream InpData;
    char filename[256];
cout<<"Enter file name:"<<endl;
cin>>filename;
InpData.open(filename,ios_base::binary);
while(InpData==0)
{
    cout<<"You entered wrong address, enter again file name:"<<endl;
    cin>>filename;
    InpData.open(filename,std::ifstream::binary);
}
InpData.seekg (0, InpData.end);
int length = InpData.tellg();
char *buffer=new char[length];
    std::cout << "Reading " << length << " characters... ";
InpData.seekg (0, InpData.beg);
InpData.read(buffer,length);
ofstream Outpdata;
Outpdata.open("results.txt");

 for(size_t i = 0; i < 1024; ++i)
{
    size_t off = i * 4 + 12;
    // parse a 32bit integer little-endian
    int32_t value = ((int32_t)(unsigned char)buffer[off + 3] << 24)
                 | ((int32_t)(unsigned char)buffer[off + 2] << 16)
                  | ((int32_t)(unsigned char)buffer[off + 1] << 8)
                | ((int32_t)(unsigned char)buffer[off + 0] << 0);
                Outpdata<<value<<endl;
             }
            InpData.close();
            Outpdata.close();
            delete[] buffer;*/
             }

1 个答案:

答案 0 :(得分:0)

你的数据是小端的(引用:“LSB优先”,第23页底部)所以你走了:

for(size_t i = 0; i < 1024; ++i)
{
    size_t off = i * 4 + 12;
    // parse a 32bit integer little-endian
    int32_t value = ((int32_t)(unsigned char)buffer[off + 3] << 24)
                  | ((int32_t)(unsigned char)buffer[off + 2] << 16)
                  | ((int32_t)(unsigned char)buffer[off + 1] << 8)
                  | ((int32_t)(unsigned char)buffer[off + 0] << 0);
    ...
}

对于浮点数,您可以使用memcpy

assert(sizeof(float)==4);
for(size_t i = 0; i < 1024; ++i)
{
    size_t off = i * 4 + 12;
    // parse a 32bit ieee floating point
    float value;
    memcpy(&value, buffer + off, 4);
    ...
}