把二进制文件转换为文本格式(十进制)

首先要清楚文件里, 储存的数据的类型,是int,long,long long 还是 float,double
其次文件有无字节序(大小端)问题
这两个问题解决了
直接按照数据类型定义一个定长数据,或者数组一次性读入,或者分批读入全部文件。
需要的话,读取以后,先转换一下字节顺序
fopen,二进制读方式,打开文件,fread 读取文件,fclose 关闭文件

然后 转换成十进制 ACII格式的数据,输出到文本文件中去。
 fopen,文本写方式,打开文件,fprintf 写入文件,fclose 关闭文件。

基本上就可以了

具体可以了解一下 fopen,fread,fwrie,fclose , fprintf ,fscanf  
这些C流式文件读写,打开,关闭函数

这些都是C标准库的函数,使用的时候 #include <stdio.h> 就可以了


C++ 可以用C++流 std::fstream ,std::ifstream,std::ofstream 做同样的事情。
使用的时候
#include <fstream>

 

为什么用笔记本打开二进制文件会出现乱码现象?

What exactly causes binary file “gibberish”?

问题:

I haven't found an answer to this particular question; perhaps there isn't one. But I've been wondering for a while about it.

What exactly causes a binary file to display as "gibberish" when you look at it in a text editor? It's the same thing with encrypted files. Are the binary values of the file trying to be converted into ASCII? Is it possible to convert the view to display raw binary values, i.e. to show the 1s and 0s that make up the file?

Finally, is there a way to determine what program will properly open a data file? Many times, especially with Windows, a file is orphaned or otherwise not associated w/ a particular program. Opening it in a text editor sometimes tells you where it belongs but most of the time doesn't, due to the gibberish. If the extension doesn't provide any information, how can you determine what program it belongs to?

回答:

  • Are the binary values of the file trying to be converted into ASCII?

Yes, that's exactly what's happening. Typically, the binary values of the file also include ASCII control characters that aren't printable, resulting in even more bizarre display in a typical text editor.

  • Is it possible to convert the view to display raw binary values, i.e. to show the 1s and 0s that make up the file?

It depends on your editor. What you want is a "hex editor", rather than a normal text editor. This will show you the raw contents of the file (typically in hexadecimal rather than binary, since the zeros and ones would take up a lot of space and be harder to read).

  • Finally, is there a way to determine what program will properly open a data file?

There is a Linux command-line program called "file" that will attempt to analyze the file (typically looking for common header patterns) and tell you what sort of file it is (for example text, or audio, or video, or XML, etc). I'm not sure if there is an equivalent program for Windows. Of course, the output of this program is just a guess, but it can be very useful when you don't know what the format of a file is.

 

示例代码:读入二进制文件再写到文本中去

在这个例子中,二进制文本大概长这样:F1 D3 35 C4 35 38 3E ...

它实质上记录的是float型的数据(float占4个字节,即每4个字节表示一个十进制里的float数据)。每个float数据其实是一个三维坐标点的一个坐标值。在下面的代码中,每次连续读入三个float值从而得到一个坐标点。主要用到函数是:

istream& read(char* buffer, int count);

这个函数表示从文件流中读数据,读多少呢?读取count个字节的数据;存到哪呢?存到参数buffer指向的内存位置。注意这个内存位置由一个字符指针表示(上面的第一个参数),在必要的时候需要强制类型转换。一般我们先定义一个变量用于保存,比如在下面代码中该参数为&coordinates[i]。每完成一次读操作,文件读指针就往后移动相应的字节。

 

代码:

#include <fstream>
#include <iostream>
using namespace std;

int main(){
   //把一个数据点的三个坐标放到一个float数组中
   const int num_of_coord = 3;
   float coordinates[num_of_coord]={0.};
   
   const char* InFileName = "data.log";
   const char* OutFileName = "data.txt";
 
   //用构造函数创建文件流对象,以二进制方式读入,以文本方式写出
   ifstream infile(InFileName, ios::binary);
   ofstream outfile(OutFileName);

   if(!(infile && outfile)){
      cout<<"open file error"<<endl;
      return -1;
   }
   
   while(!infile.eof()){
      //每次循环读入三个坐标值,每个坐标值对应的二进制数据长度其实就是float的字节数
      for(int i=0; i<num_of_coord; i++){
         infile.read((char*)&coordinates[i], sizeof(float));
         cout<<coordinates[i]<<", ";
      }
      cout<<endl;

      for(int i=0; i<num_of_coord; i++)
         outfile<<coordinates[i]<<", ";
      outfile<<endl;  //每输出一个点的三个坐标值,换行一次
   }

   infile.close();
   outfile.close();
   return 0;
}

这里稍微解释一下infile.read((char*)&coordinates[i], sizeof(float));:

coordinates[i]实际上就是个float类型,&coordinates[i]就是取它的内存地址,换句话说是指向float(4个字节)的类型指针。(char*)&coordinates[i]是加上强制类型转换,将该地址由float*转换为char*,指针类型转换为指向一个字节的字符指针。想想这是为什么?

 

下面这个链接用一个简单例子说明了c++二进制文件读取和写出的用法

c.biancheng.net/view/302.html

 

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将二进制dat文件转换十进制的UTF-8文本文件,你需要读取二进制文件中的数据,并将其解析为UTF-8字符,然后将这些字符的十进制表示写入文本文件。下面是一个示例代码,展示了如何在C++中实现这个功能: ```cpp #include <iostream> #include <fstream> #include <vector> int main() { std::ifstream inputFile("input.dat", std::ios::binary); if (!inputFile) { std::cout << "Failed to open input file." << std::endl; return 1; } std::ofstream outputFile("output.txt"); if (!outputFile) { std::cout << "Failed to create output file." << std::endl; return 1; } // 逐个字节读取二进制文件内容,并将其转换为UTF-8编码后写入文本文件 char byte; std::vector<char> utf8Bytes; while (inputFile.read(&byte, sizeof(byte))) { utf8Bytes.push_back(byte); } // 将UTF-8字节序列转换十进制表示,并写入文本文件 for (char utf8Byte : utf8Bytes) { int decimalValue = static_cast<int>(static_cast<unsigned char>(utf8Byte)); outputFile << decimalValue << " "; } inputFile.close(); outputFile.close(); std::cout << "Binary file converted to decimal UTF-8 text successfully." << std::endl; return 0; } ``` 上述代码假设你的二进制dat文件名为"input.dat",转换后的十进制UTF-8文本将保存在"output.txt"中。每个UTF-8字符的十进制表示之间用空格隔开。 请注意,上述代码假设dat文件中的数据是以UTF-8编码格式保存的。如果你的dat文件采用其他编码格式,需要根据实际情况进行相应的解码和转换。 希望这个示例对你有所帮助!如果你有更多的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值