1. 程式人生 > >C++和Java位元組高低位的轉換(大小端)

C++和Java位元組高低位的轉換(大小端)

從一個C/C++語言生成的二進位制檔案中讀出一個float資料 

// 參見java.io.DataInputStream  
// C++寫入的位元組順序是從低到高(左低到右高),  
   而java.io.DataInputStream讀取的資料是從高到低(左高到右低)  
// 所以需要自己改寫一下  
// 功能和java.io.DataInputStream類似的  
public class CppInputStream extends FilterInputStream {  
  
  public CppInputStream(InputStream in) {  
    super(in);  
  }  
  public final int read(byte b[]) throws IOException {  
    return in.read(b, 0, b.length);  
  }  
  
  public final int read(byte b[], int off, int len) throws IOException {  
    return in.read(b, off, len);  
  }  
  
  public final void readFully(byte b[]) throws IOException {  
    readFully(b, 0, b.length);  
  }  
  
  public final void readFully(byte b[], int off, int len) throws IOException {  
    if (len < 0)  
      throw new IndexOutOfBoundsException();  
    int n = 0;  
    while (n < len) {  
      int count = in.read(b, off + n, len - n);  
      if (count < 0)  
        throw new EOFException();  
      n += count;  
    }  
  }  
  
  public final int skipBytes(int n) throws IOException {  
    int total = 0;  
    int cur = 0;  
    while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) {  
      total += cur;  
    }  
    return total;  
  }  
  
  public final byte readByte() throws IOException {  
    int ch = in.read();  
    if (ch < 0)  
      throw new EOFException();  
    return (byte) (ch);  
  }  
  
  public final int readUnsignedByte() throws IOException {  
    int ch = in.read();  
    if (ch < 0)  
      throw new EOFException();  
    return ch;  
  }  
  
  public final short readShort() throws IOException {  
    int ch2 = in.read();  
    int ch1 = in.read();  
    if ((ch1 | ch2) < 0)  
      throw new EOFException();  
    return (short) ((ch1 << 8) + (ch2 << 0));  
  }  
  
  public final int readUnsignedShort() throws IOException {  
    int ch2 = in.read();  
    int ch1 = in.read();  
    if ((ch1 | ch2) < 0)  
      throw new EOFException();  
    return (ch1 << 8) + (ch2 << 0);  
  }  
  
  public final char readChar() throws IOException {  
    int ch2 = in.read();  
    int ch1 = in.read();  
    if ((ch1 | ch2) < 0)  
      throw new EOFException();  
    return (char) ((ch1 << 8) + (ch2 << 0));  
  }  
  
  public final int readInt() throws IOException {  
    int ch4 = in.read();  
    int ch3 = in.read();  
    int ch2 = in.read();  
    int ch1 = in.read();  
    if ((ch1 | ch2 | ch3 | ch4) < 0)  
      throw new EOFException();  
    return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));  
  }  
  
  private byte readBuffer[] = new byte[8];  
  
  public final long readLong() throws IOException {  
    readFully(readBuffer, 0, 8);  
    return (((long) readBuffer[7] << 56) + ((long) (readBuffer[6] & 255) << 48)  
        + ((long) (readBuffer[5] & 255) << 40) + ((long) (readBuffer[4] & 255) << 32)  
        + ((long) (readBuffer[3] & 255) << 24) + ((readBuffer[2] & 255) << 16)  
        + ((readBuffer[1] & 255) << 8) + ((readBuffer[0] & 255) << 0));  
  }  
  
  public final float readFloat() throws IOException {  
    return Float.intBitsToFloat(readInt());  
  }  
  
  public final double readDouble() throws IOException {  
    return Double.longBitsToDouble(readLong());  
  }  
}  

也可以先用Java讀取一個Int進來,然後處理 
// Java讀取後,順序已經反了  
int javaReadInt = ;  
  
// 將每個位元組取出來  
byte byte4 = (byte) (javaReadInt & 0xff);  
byte byte3 = (byte) ((javaReadInt & 0xff00) >> 8);  
byte byte2 = (byte) ((javaReadInt & 0xff0000) >> 16);  
byte byte1 = (byte) ((javaReadInt & 0xff000000) >> 24);  
  
// 拼裝成 正確的int  
int realint = (byte1& 0xff)<<0  + (byte2& 0xff)<<8 + (byte3& 0xff)<< 16 +(byte4& 0xff)<<24 ;  

另外可以使用ByteBuffer來完成,而不需要自己考慮,如何將位元組陣列轉換為其他資料型別. 使用ByteBuffer,可以設定位元組順序. 

ByteBuffer簡單的例子 

import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 

public class ByteBufferTest { 

  public static void main(String[] args) { 
    //將位元組陣列轉換為int型別 
    byte[] bytes = {0,0,0,1}; 
    ByteBuffer buffer =  ByteBuffer.wrap(bytes); 
    System.out.println(buffer.getInt()); 
   
    ByteBuffer buffer2 = ByteBuffer.wrap(bytes); 
    buffer2.order(ByteOrder.LITTLE_ENDIAN); 
    System.out.println(buffer2.getInt()); 
    
  } 

}