2011-05-03 93 views
8

按照https://stackoverflow.com/questions/1738244/what-is-the-java-equivalent-of-net-bitconverter問題中提供的建議,我已經開始實施我自己的Java轉換器,但沒有得到相同的結果。用於Java的Bitconverter

有人請指導我,我可能會做錯嗎?

public static byte[] GetBytes(Integer value) { 
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
    DataOutputStream stream = new DataOutputStream(byteStream); 
    try { 
     stream.writeInt(value); 
    } catch (IOException e) { 
     return new byte[4]; 
    } 
    return byteStream.toByteArray(); 
} 

byte[] result = BitConverter.GetBytes(1234); //JAVA: [0, 0, 4, -46] 
byte[] result = BitConverter.GetBytes(1234); //C#: [210, 4, 0, 0] 

回答

8

這只是字節順序(-46和210是因爲Java的簽名字節,但這只是一個UI的東西)。要麼顛倒數組內容,要麼使用移位操作來寫入int。

注意:.NET發出的排列順序取決於平臺。我建議在兩種情況下都使用KNOWN ENDIANNESS;最有可能的是通過使用兩者的移位操作。或者更好的主意:只需使用預先封裝的,獨立於平臺的序列化格式(例如:對Java和.NET/C#都具有良好支持的協議緩衝區)。

例如;如果我寫的int valuebyte[] buffer(在offset開始),我可能會使用:

buffer[offset++] = (byte)value; 
buffer[offset++] = (byte)(value>>8); 
buffer[offset++] = (byte)(value>>16); 
buffer[offset++] = (byte)(value>>24); 

這是保證小尾數,以及類似的代碼應該在任何框架的工作。

+0

什麼是關閉設定值? – gpa 2013-08-09 22:11:24

+0

@gpa不管你到目前爲止閱讀的數據如何 – 2013-08-09 22:19:21

4

C#BitConverter將使用基礎架構的字節序。在大多數環境中,它將是小端(就像你的情況一樣)。然而,Java的DataOutputStream將總是以big-endian(「便攜式方式」)編寫。如果您想匹配行爲,您需要檢查機器的字節順序並進行相應的編寫。

此外,java中的字節是有符號的,所以輸出只是一個裝飾差異。位表示是相同的,所以你不必擔心這一點。

要檢查機器的字節順序,請使用java.nio.ByteOrder.nativeOrder()方法。然後使用java.nio.ByteBuffer代替指定字節order()並寫入數據的位置。

然後,您可以實現你的方法是這樣的:

public static byte[] GetBytes(int value) 
{ 
    ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()); 
    buffer.putInt(value); 
    return buffer.array(); 
} 
1

建立在傑夫的回答,您可以使用一個ByteBuffer既轉換和從intbyte[]。下面是代碼,你可以拖放到一個類轉換到/來自小尾:

ByteBuffer _intShifter = ByteBuffer.allocate(Integer.SIZE/Byte.SIZE) 
            .order(ByteOrder.LITTLE_ENDIAN); 

public byte[] intToByte(int value) { 
    _intShifter.clear(); 
    _intShifter.putInt(value);  
    return _intShifter.array(); 
} 

public int byteToInt(byte[] data) 
{ 
    _intShifter.clear(); 
    _intShifter.put(data, 0, Integer.SIZE/Byte.SIZE); 
    _intShifter.flip(); 
    return _intShifter.getInt(); 
} 
1

,如果任何機構need..C#到JAVA BitConverter.ToInt32

public static int toInt32_2(byte[] bytes, int index) 
     { 
      int a = (int)((int)(0xff & bytes[index]) << 32 | (int)(0xff & bytes[index + 1]) << 40 | (int)(0xff & bytes[index + 2]) << 48 | (int)(0xff & bytes[index + 3]) << 56); 
      // int a = (int)((int)(0xff & bytes[index]) << 56 | (int)(0xff & bytes[index + 1]) << 48 | (int)(0xff & bytes[index + 2]) << 40 | (int)(0xff & bytes[index + 3]) << 32); 
      //Array.Resize; 
      return a; 
     } 

而且Int16的

public static short toInt16(byte[] bytes, int index) //throws Exception 
    { 
     return (short)((bytes[index + 1] & 0xFF) | ((bytes[index] & 0xFF) << 0)); 
     //return (short)(
     //  (0xff & bytes[index]) << 8 | 
     //    (0xff & bytes[index + 1]) << 0 
     //); 
    } 

BitConverter.getBytes

public static byte[] GetBytesU16(long value) 
{ 

    ByteBuffer buffer = ByteBuffer.allocate(8).order(ByteOrder.nativeOrder()); 
    buffer.putLong(value); 
    return buffer.array(); 
}