把二进制文件转换为文本格式(十进制) 您所在的位置:网站首页 6eh转化成十进制 把二进制文件转换为文本格式(十进制)

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

2023-08-13 08:02| 来源: 网络整理| 查看: 265

首先要清楚文件里, 储存的数据的类型,是int,long,long long 还是 float,double 其次文件有无字节序(大小端)问题 这两个问题解决了 直接按照数据类型定义一个定长数据,或者数组一次性读入,或者分批读入全部文件。 需要的话,读取以后,先转换一下字节顺序 fopen,二进制读方式,打开文件,fread 读取文件,fclose 关闭文件 然后 转换成十进制 ACII格式的数据,输出到文本文件中去。  fopen,文本写方式,打开文件,fprintf 写入文件,fclose 关闭文件。 基本上就可以了 具体可以了解一下 fopen,fread,fwrie,fclose , fprintf ,fscanf   这些C流式文件读写,打开,关闭函数 这些都是C标准库的函数,使用的时候 #include  就可以了

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

 

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

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 #include 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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有