我正在將一些東西從C#移植到JAVA,我需要一個可以將字節轉換爲基元的類,只需在.NET中使用BitConverter即可。請驗證:該轉換器將字節數組讀作Big-Endian?
正如我剛剛發佈here,我注意到,我的電腦使用小端(英特爾)和BitConverter按預期工作:
// C# code
byte[] b2 = new byte[] { 0, 1 };
short b2short = BitConverter.ToInt16(b2, 0);
的 b2short == 256如預期。
那麼,我需要一個JAVA的「BitConverter」,發現this piece of code。但是,當我在JAVA中嘗試它時,在我看來,這些方法將字節數組解釋爲Big-Endian。 JAVA代碼:
// JAVA code
byte[] b2 = new byte[] { 0, 1 };
short b2short = MyFoundConverter.BitConverter.toShort(b2, 0);
在這種情況下, b2short == 1,而在我看來,像大端。
問題:有人可以知道那些按位運算符,告訴我是否確實發現代碼on this webpage正在將字節數組解釋爲Big-Endian?
如果是這樣,,有沒有一種簡單的方法來使它Little-Endian?
問候
Thx,我選擇通過交換數組索引來手動完成。 IT幾分鐘工作,然後繼續=) – Ted 2010-01-07 21:51:01