java--byte與基礎類型轉換

來源:互聯網
上載者:User

標籤:數組   put   pre   return   定義   oat   getch   rect   java   

byte b[]={0x41,(byte) 0xc8,0,0}; //方法1 流輸入,適用於ME/SE環境//預設大端數,如果小端數,可以先翻轉數組DataInputStream dis=new DataInputStream(new ByteArrayInputStream(b));float f=dis.readFloat();dis.close();System.out.println(f);//方法2 緩衝輸入,適用於SE/EE環境ByteBuffer buf=ByteBuffer.allocateDirect(4); //無額外記憶體的直接緩衝//buf=buf.order(ByteOrder.LITTLE_ENDIAN);//預設大端,小端用這行buf.put(b);buf.rewind();float f2=buf.getFloat();System.out.println(f2);
//方法三,自訂工具類package cn.yuehua.io;/** * byte[] 轉換 * 採用大端順序,即對於0x11223344,byte[0]儲存0x11,byte[1]儲存0x22,byte[2]儲存0x33,byte[3]儲存0x44 */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處的連續兩個位元組獲得一個char    public 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處的連續兩個位元組獲得一個short    public 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個位元組獲得一個int    public 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個位元組獲得一個float    public 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個位元組獲得一個long    public 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個位元組獲得一個double    public static double getDouble(byte[] arr, int index) {        return Double.longBitsToDouble(getLong(arr, index));    }    public static void main(String[] args) {        float f = 11.9f;        byte[] byteArray = getByteArray(f);        float aFloat = getFloat(byteArray, 0);        System.out.println(aFloat);    }}

 

java--byte與基礎類型轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.