[分享]Java中Byte與基礎類型之間的轉換

來源:互聯網
上載者:User

標籤:blink   socket   byte   java-bit   

========================================================
qiujuer
部落格:blog.csdn.net/qiujuer
網站:www.qiujuer.net
開源庫:github.com/qiujuer/Genius-Android
開源庫:github.com/qiujuer/Blink
轉載請註明出處:http://blog.csdn.net/qiujuer/article/details/45152189
——學之開源,用於開源;初學者的心態,與君共勉!

========================================================

很久沒有寫部落格了,有些生疏了;一直忙著做 Blink 架構去了。
在這裡打個廣告,Blink是一款Socket協議跨平台架構,現在提供有Java、Android、C#的版本;通過Blink能實現對Socket傳輸的非同步封裝,能直接發送或接受資料,現能直接發送 byte ,String,File 等類型,可在不改動架構情況下直接擴充所需類型。

Blink開源架構連結

因為在Socket的傳輸中都是 Bytes 的傳輸,所以涉及到 Java 基本類型(char、short、int、long)與byte[] 的轉化,自己總結了一下,簡單的寫了一個類。

/** * Bit Converter */public class BitConverter {    /**     * Convert char to byte[]     *     * @param x char     * @return bytes     */    public static byte[] toBytes(char x) {        return toBytes(x, new byte[2], 0);    }    /**     * Convert char to byte[]     *     * @param x       char     * @param bytes   Dest bytes     * @param bytePos Dest pos     * @return bytes     */    public static byte[] toBytes(char x, byte[] bytes, int bytePos) {        bytes[bytePos++] = (byte) (x);        bytes[bytePos] = (byte) (x >> 8);        return bytes;    }    /**     * Convert short to byte[]     *     * @param x Short     * @return bytes     */    public static byte[] toBytes(short x) {        return toBytes(x, new byte[2], 0);    }    /**     * Convert short to byte[]     *     * @param x       Short     * @param bytes   Dest bytes     * @param bytePos Dest pos     * @return bytes     */    public static byte[] toBytes(short x, byte[] bytes, int bytePos) {        bytes[bytePos++] = (byte) (x);        bytes[bytePos] = (byte) (x >> 8);        return bytes;    }    /**     * Convert int to byte[]     *     * @param x int     * @return bytes     */    public static byte[] toBytes(int x) {        return toBytes(x, new byte[4], 0);    }    /**     * Convert int to byte[]     *     * @param x       int     * @param bytes   Dest bytes     * @param bytePos Dest pos     * @return bytes     */    public static byte[] toBytes(int x, byte[] bytes, int bytePos) {        bytes[bytePos++] = (byte) (x);        bytes[bytePos++] = (byte) (x >> 8);        bytes[bytePos++] = (byte) (x >> 16);        bytes[bytePos] = (byte) (x >> 24);        return bytes;    }    /**     * Convert long to byte[]     *     * @param x long     * @return bytes     */    public static byte[] toBytes(long x) {        return toBytes(x, new byte[8], 0);    }    /**     * Convert long to byte[]     *     * @param x       long     * @param bytes   Dest bytes     * @param bytePos Dest pos     * @return bytes     */    public static byte[] toBytes(long x, byte[] bytes, int bytePos) {        bytes[bytePos++] = (byte) (x);        bytes[bytePos++] = (byte) (x >> 8);        bytes[bytePos++] = (byte) (x >> 16);        bytes[bytePos++] = (byte) (x >> 24);        bytes[bytePos++] = (byte) (x >> 32);        bytes[bytePos++] = (byte) (x >> 40);        bytes[bytePos++] = (byte) (x >> 48);        bytes[bytePos] = (byte) (x >> 56);        return bytes;    }    /**     * Convert byte[] to char     *     * @param bytes bytes     * @return char     */    public static char toChar(byte[] bytes) {        return toChar(bytes, 0);    }    /**     * Convert byte[] to char     *     * @param bytes bytes     * @param index byte start index     * @return char     */    public static char toChar(byte[] bytes, int index) {        return (char) ((bytes[index + 1] << 8) | (bytes[index] & 0xff));    }    /**     * Convert byte[] to short     *     * @param bytes bytes     * @return short     */    public static short toShort(byte[] bytes) {        return toShort(bytes, 0);    }    /**     * Convert byte[] to short     *     * @param bytes bytes     * @param index byte start index     * @return short     */    public static short toShort(byte[] bytes, int index) {        return (short) ((bytes[index + 1] << 8) | (bytes[index] & 0xff));    }    /**     * Convert byte[] to int     *     * @param bytes bytes     * @return int     */    public static int toInt(byte[] bytes) {        return toInt(bytes, 0);    }    /**     * Convert byte[] to int     *     * @param bytes bytes     * @param index bytes start index     * @return int     */    public static int toInt(byte[] bytes, int index) {        return (((bytes[index + 3]) << 24) |                ((bytes[index + 2] & 0xff) << 16) |                ((bytes[index + 1] & 0xff) << 8) |                ((bytes[index] & 0xff)));    }    /**     * Convert byte[] to long     *     * @param bytes bytes     * @return long     */    public static long toLong(byte[] bytes) {        return toLong(bytes, 0);    }    /**     * Convert byte[] to long     *     * @param bytes bytes     * @param index bytes start index     * @return long     */    public static long toLong(byte[] bytes, int index) {        return ((((long) bytes[index + 7]) << 56) |                (((long) bytes[index + 6] & 0xff) << 48) |                (((long) bytes[index + 5] & 0xff) << 40) |                (((long) bytes[index + 4] & 0xff) << 32) |                (((long) bytes[index + 3] & 0xff) << 24) |                (((long) bytes[index + 2] & 0xff) << 16) |                (((long) bytes[index + 1] & 0xff) << 8) |                (((long) bytes[index] & 0xff)));    }}

需要注意的是:無論是從基本類型轉換為 byte[] 還是 byte[] 轉換為基本類型, 都是採用的低位在前高位在後的形式;這個與C#的預設模式是一致的,如果你的需求是高位在前的情況則需要自己更改一下順序了。

========================================================
qiujuer
部落格:blog.csdn.net/qiujuer
網站:www.qiujuer.net
開源庫:github.com/qiujuer/Genius-Android
開源庫:github.com/qiujuer/Blink
轉載請註明出處:http://blog.csdn.net/qiujuer/article/details/45152189
——學之開源,用於開源;初學者的心態,與君共勉!

========================================================

[分享]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.