標籤:java 資料
最近在寫C++ socket和java socket之間的通訊程式,涉及到整數浮點數的傳輸,需要從位元組數組還原資料,查了一些資料,總結如下
1. 整數和浮點數的機器表示
在機器內部,不論是一個整數還是浮點數,都是以一個二進位串的形式儲存。
整數可能是原碼,補碼錶示,浮點數有階碼尾數兩部分構成,無論怎樣都是一個二進位串,但是這個二進位串如何表示不同的機器可能採取不同的方案。
關於浮點數:
一個機器上的32位浮點數的二進位串在另一個機器把它作為32位浮點數解釋時可能得到不同的值,這樣不同機器上進行網路傳輸的時候,大概就只能用傳字串的方式了,不過幸好,IEEE 754規範標準化了浮點數的表示形式,遵從這個標準的不同程式設計語言裡只要遵從這個標準,那麼某個浮點數的32位二進位串都是一樣的,,,和CPU無關,和語言無關
關於整數 :
單個位元組的整型值就是一個位元組;佔用多個位元組的整數,其表示有所區別,有所謂的小端法,大端法,具體表示可以看下面的圖。
2. 小端法、大端法和主機位元組順序、網路位元組順序
小端法和大端法
網路位元組序就是大端順序,因為TCP/IP首部中所有的二進位整數在網路中傳輸時都要求以這種次序,因此它又稱作網路位元組序。
主機位元組順序就是指相對於網路傳輸是的位元組順序的主機上的位元組順序,有大端標記法,小端標記法。
3. 下面給出java中的基礎資料型別 (Elementary Data Type)和位元組數組byte[]之間的轉碼
本文中byte[]的順序按照“大端順序”,這句話的意思是說對於整數0x11223344
byte[0]儲存0x11,byte[1]儲存0x22,byte[2]儲存0x33,byte[3]儲存0x44
採用這種“大端順序”,由byte[]轉int,或由int轉byte[],這裡面byte的順序都需要是上面描述的“大端順序”,這樣好處是伺服器發送時可以調用htonl,htons等函數將位元組順序轉換為網路位元組順序,其實關鍵是伺服器和用戶端要有一個一致的表示規範,看起來似乎網路位元組順序更順眼一些?
Java對於各種基本類型的長度做了規定,所以對應byte數組的長度也固定,和機器無關。
下面是char,short,int,float,long,double轉換的代碼,核心是利用位操作,取出某個位元組中的8個位,進行轉換。
很大程度上參考了http://tjmljw.iteye.com/blog/1767716一文中的實現(該文採用的是“小端順序”的byte數組,我用的是“大端順序”)。
import java.nio.ByteOrder;public class ByteArrayConveter {// char轉換為byte[2]數組public static byte[] getByteArray(char c) {byte[] b = new byte[2];b[0] = (byte) ((c & 0xff00) >> 8);b[1] = (byte) (c & 0x00ff);return b;}// 從byte數組的index處的連續兩個位元組獲得一個charpublic static char getChar(byte[] arr, int index) {return (char) (0xff00 & arr[index] << 8 | (0xff & arr[index + 1]));}// short轉換為byte[2]數組public static byte[] getByteArray(short s) {byte[] b = new byte[2];b[0] = (byte) ((s & 0xff00) >> 8);b[1] = (byte) (s & 0x00ff);return b;}// 從byte數組的index處的連續兩個位元組獲得一個shortpublic static short getShort(byte[] arr, int index) {return (short) (0xff00 & arr[index] << 8 | (0xff & arr[index + 1]));}// int轉換為byte[4]數組public static byte[] getByteArray(int i) {byte[] b = new byte[4];b[0] = (byte) ((i & 0xff000000) >> 24);b[1] = (byte) ((i & 0x00ff0000) >> 16);b[2] = (byte) ((i & 0x0000ff00) >> 8);b[3] = (byte) (i & 0x000000ff);return b;}// 從byte數組的index處的連續4個位元組獲得一個intpublic static int getInt(byte[] arr, int index) {return (0xff000000 & (arr[index+0] << 24)) | (0x00ff0000 & (arr[index+1] << 16)) | (0x0000ff00 & (arr[index+2] << 8)) | (0x000000ff & arr[index+3]);}// float轉換為byte[4]數組public static byte[] getByteArray(float f) {int intbits = Float.floatToIntBits(f);//將float裡面的二進位串解釋為int整數return getByteArray(intbits);}// 從byte數組的index處的連續4個位元組獲得一個floatpublic static float getFloat(byte[] arr, int index) {return Float.intBitsToFloat(getInt(arr, index));}// long轉換為byte[8]數組public static byte[] getByteArray(long l) {byte b[] = new byte[8];b[0] = (byte) (0xff & (l >> 56));b[1] = (byte) (0xff & (l >> 48));b[2] = (byte) (0xff & (l >> 40));b[3] = (byte) (0xff & (l >> 32));b[4] = (byte) (0xff & (l >> 24));b[5] = (byte) (0xff & (l >> 16));b[6] = (byte) (0xff & (l >> 8));b[7] = (byte) (0xff & l);return b;}// 從byte數組的index處的連續8個位元組獲得一個longpublic static long getLong(byte[] arr, int index) {return (0xff00000000000000L & ((long)arr[index+0] << 56)) | (0x00ff000000000000L & ((long)arr[index+1] << 48)) | (0x0000ff0000000000L & ((long)arr[index+2] << 40)) | (0x000000ff00000000L & ((long)arr[index+3] << 32)) |(0x00000000ff000000L & ((long)arr[index+4] << 24)) | (0x0000000000ff0000L & ((long)arr[index+5] << 16)) | (0x000000000000ff00L & ((long)arr[index+6] << 8)) | (0x00000000000000ffL & (long)arr[index+7]);}// double轉換為byte[8]數組public static byte[] getByteArray(double d) {long longbits = Double.doubleToLongBits(d);return getByteArray(longbits);}// 從byte數組的index處的連續8個位元組獲得一個doublepublic static double getDouble(byte[] arr, int index) {return Double.longBitsToDouble(getLong(arr, index));}public static void main(String[] args) {System.out.println(ByteOrder.nativeOrder());if(args.length < 1){System.out.println("enter 'char' test method about char");System.out.println("enter 'short' test method about short");System.out.println("enter 'int' test method about int");System.out.println("enter 'float' test method about float");System.out.println("enter 'long' test method about long");System.out.println("enter 'double' test method about double");return;}if(args[0].equals("char")){char c = '\u0000';while( c < '\uffff'){System.out.println(getChar(getByteArray(c),0));c++;}}else if(args[0].equals("short")){short s = Short.MIN_VALUE;while( s < Short.MAX_VALUE){System.out.println(getShort(getByteArray(s), 0));s++;}}else if(args[0].equals("int")){int i = Integer.MIN_VALUE;while( i < Integer.MAX_VALUE){System.out.println(getInt(getByteArray(i), 0));i++;}}else if(args[0].equals("float")){float f = Float.MIN_VALUE;while(f < Float.MAX_VALUE){System.out.println(getFloat(getByteArray(f), 0));f+=1.1111f;}}else if(args[0].equals("long")){long l = Long.MIN_VALUE;while(l < Long.MAX_VALUE){System.out.println(getLong(getByteArray(l), 0));l++;}}else if(args[0].equals("double")){double d = Double.MIN_VALUE;while(d < Double.MAX_VALUE){System.out.println(getDouble(getByteArray(d), 0));d+=1.111D;}}}}
說明:
本文由giantpoplar發表於CSDN
文章地址 http://write.blog.csdn.net/postedit/47657333
轉載請保留本說明
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
位元組數組byte[]和整型,浮點型資料的轉換——Java代碼