如何使用 JAVA 读取 C/Matlab 创建的二进制文件

How to read binary file created by C/Matlab using JAVA(如何使用 JAVA 读取 C/Matlab 创建的二进制文件)
本文介绍了如何使用 JAVA 读取 C/Matlab 创建的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用以下 matlab 代码创建了一个二进制文件:

I have created a binary file using the following matlab code:

x is an array of int32 numbers
n is the length of x

fid = fopen("binary_file.dat", "wb");
fwrite(fid, n, 'int32');
fwrite(fid, x, 'int32');
fclose(fid);

我可以使用下面的 C 代码来读取这个文件:

I can use the following C code to read this file:

fp = fopen("binary_file.dat", "rb");
int n;
fread(&n, 4, 1, fp);//read 4 bytes
int *x = new int[n];
for (int i = 0; i < n; i++)
{
int t;
fread(&t,4, 1,fp);//read 4 bytes
x[i] = t;
}
......

上面的 C 代码可以读取正确的结果.但是,我现在想用 JAVA 读取这样的二进制文件.我的代码如下所示:

The above C code can read correct results. However, I now want to read such binary file in JAVA. My code is shown as follows:

DataInputStream data_in = new DataInputStream(
             new BufferedInputStream(
                    new FileInputStream(
                new File("binary_file.dat"))));
while(true)
{
   try {
      int t = data_in.readInt();//read 4 bytes
      System.out.println(t);
   } catch (EOFException eof) {
    break;
   }
}
data_in.close();

它确实在 n+1 次循环后终止,但结果不正确.谁能帮帮我.非常感谢!

It DOES terminates after n+1 loops, but the results are not correct. Can anybody help me out. Thanks very much!

推荐答案

我猜这是字节序问题,即你的二进制文件写成小端整数(可能是因为您使用的是 Intel 或类似的 CPU).

As I was guessing it is an endianness issue, i.e. your binary file is written as little-endian integers (probably, because you are using a Intel or similar CPU).

然而,Java 代码正在读取大端整数,无论它在什么 CPU 上运行.

The Java code, however, is reading big-endian integers, no matter what CPU it is running on.

为了显示问题,以下代码将读取您的数据并将整数显示为字节序转换前后的十六进制数.

To show the problem the following code will read your data and display the integers as hex number before and after endianness conversion.

import java.io.*;

class TestBinaryFileReading {

  static public void main(String[] args) throws IOException {  
    DataInputStream data_in = new DataInputStream(
        new BufferedInputStream(
            new FileInputStream(new File("binary_file.dat"))));
    while(true) {
      try {
        int t = data_in.readInt();//read 4 bytes

        System.out.printf("%08X ",t); 

        // change endianness "manually":
        t = (0x000000ff & (t>>24)) | 
            (0x0000ff00 & (t>> 8)) | 
            (0x00ff0000 & (t<< 8)) | 
            (0xff000000 & (t<<24));
        System.out.printf("%08X",t); 
        System.out.println();
      } 
      catch (java.io.EOFException eof) {
        break;
      }
    } 
    data_in.close();
  }
}

如果您不想手动"更改字节顺序,请参阅此问题的答案问题:
将小端文件转换为大端

If you don't want to do change endianness "manually", see answers to this question:
convert little Endian file into big Endian

这篇关于如何使用 JAVA 读取 C/Matlab 创建的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android schema validation(Android 架构验证)
Required Multiple beans of same type in Spring(Spring中需要多个相同类型的bean)
How to deal with JAXB ComplexType with MixedContent data?(如何处理带有 MixedContent 数据的 JAXB ComplexType?)
Validate an XML File Against Multiple Schema Definitions(针对多个模式定义验证 XML 文件)
JAXB - Property quot;Valuequot; is already defined. Use lt;jaxb:propertygt; to resolve this conflict(JAXB - 属性“值;已经定义了.使用 lt;jaxb:propertygt;解决这个冲突)
XML instance generation from XML schema (xsd)(从 XML 模式 (xsd) 生成 XML 实例)