久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 JAVA 讀取 C/Matlab 創(chuàng)建的二進制文件

How to read binary file created by C/Matlab using JAVA(如何使用 JAVA 讀取 C/Matlab 創(chuàng)建的二進制文件)
本文介紹了如何使用 JAVA 讀取 C/Matlab 創(chuàng)建的二進制文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我使用以下 matlab 代碼創(chuàng)建了一個二進制文件:

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 代碼可以讀取正確的結(jié)果.但是,我現(xiàn)在想用 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 次循環(huán)后終止,但結(jié)果不正確.誰能幫幫我.非常感謝!

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

推薦答案

我猜這是字節(jié)序問題,即你的二進制文件寫成小端整數(shù)(可能是因為您使用的是 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 代碼正在讀取大端整數(shù),無論??它在什么 CPU 上運行.

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

為了顯示問題,以下代碼將讀取您的數(shù)據(jù)并將整數(shù)顯示為字節(jié)序轉(zhuǎn)換前后的十六進制數(shù).

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();
  }
}

如果您不想手動"更改字節(jié)順序,請參閱此問題的答案問題:
將小端文件轉(zhuǎn)換為大端

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

這篇關(guān)于如何使用 JAVA 讀取 C/Matlab 創(chuàng)建的二進制文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 男人的天堂久久 | 精品av| 国产一区二区三区网站 | 激情五月婷婷 | a级片在线观看 | 99精品在线 | 一区二区三区在线观看视频 | 国产中文字幕在线观看 | 国产精品久久久久久亚洲调教 | 91免费高清视频 | 一级片视频免费观看 | 国产精品资源在线 | 在线免费观看黄色 | 久久精品久久久久久 | 国产在线1区 | 国产精品观看 | 亚洲一二三在线 | www日本高清 | www.国产| 国产成人高清在线观看 | 欧美性网| 久久国产精品一区二区三区 | 国产香蕉视频在线播放 | 在线中文字幕第一页 | 国产午夜精品一区二区三区 | 一级毛片播放 | 一区二区三区中文字幕 | 夜夜草 | 精品国产欧美 | 日韩一区二区福利视频 | 在线观看国产视频 | 成人精品在线观看 | 伊人二区 | 91在线视频国产 | 91性高湖久久久久久久久_久久99 | 国产一区二区视频在线 | 欧美精品v国产精品v日韩精品 | 国产激情在线看 | 欧美三级在线 | 国产精品久久久久久久岛一牛影视 | 亚洲视频在线观看 |