Java 中 byte、byte 數組和 int、long 之間的轉換

來源:互聯網
上載者:User

        Java 中 byte 和 int 之間的轉換源碼:

//byte 與 int 的相互轉換public static byte intToByte(int x) {return (byte) x;}public static int byteToInt(byte b) {//Java 總是把 byte 當做有符處理;我們可以通過將其和 0xFF 進行二進位與得到它的無符值return b & 0xFF;}

        測試代碼:

//測試 int 轉 byteint int0 = 234;byte byte0 = intToByte(int0);System.out.println("byte0=" + byte0);//byte0=-22//測試 byte 轉 intint int1 = byteToInt(byte0);System.out.println("int1=" + int1);//int1=234

        Java 中 byte 數組和 int 之間的轉換源碼:

//byte 數組與 int 的相互轉換public static int byteArrayToInt(byte[] b) {    return   b[3] & 0xFF |            (b[2] & 0xFF) << 8 |            (b[1] & 0xFF) << 16 |            (b[0] & 0xFF) << 24;}public static byte[] intToByteArray(int a) {    return new byte[] {        (byte) ((a >> 24) & 0xFF),        (byte) ((a >> 16) & 0xFF),           (byte) ((a >> 8) & 0xFF),           (byte) (a & 0xFF)    };}

        測試代碼:

//測試 int 轉 byte 數組int int2 = 1417;byte[] bytesInt = intToByteArray(int2);System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced//測試 byte 數組轉 intint int3 = byteArrayToInt(bytesInt);System.out.println("int3=" + int3);//int3=1417

        Java 中 byte 數組和 long 之間的轉換源碼:

private static ByteBuffer buffer = ByteBuffer.allocate(8); //byte 數組與 long 的相互轉換    public static byte[] longToBytes(long x) {        buffer.putLong(0, x);        return buffer.array();    }    public static long bytesToLong(byte[] bytes) {        buffer.put(bytes, 0, bytes.length);        buffer.flip();//need flip         return buffer.getLong();    }

        測試代碼:

//測試 long 轉 byte 數組long long1 = 2223;byte[] bytesLong = longToBytes(long1);System.out.println("bytes=" + bytesLong);//bytes=[B@c17164//測試 byte 數組 轉 longlong long2 = bytesToLong(bytesLong);System.out.println("long2=" + long2);//long2=2223

        整體工具類源碼:

import java.nio.ByteBuffer;public class Test {private static ByteBuffer buffer = ByteBuffer.allocate(8);    public static void main(String[] args) {//測試 int 轉 byteint int0 = 234;byte byte0 = intToByte(int0);System.out.println("byte0=" + byte0);//byte0=-22//測試 byte 轉 intint int1 = byteToInt(byte0);System.out.println("int1=" + int1);//int1=234//測試 int 轉 byte 數組int int2 = 1417;byte[] bytesInt = intToByteArray(int2);System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced//測試 byte 數組轉 intint int3 = byteArrayToInt(bytesInt);System.out.println("int3=" + int3);//int3=1417//測試 long 轉 byte 數組long long1 = 2223;byte[] bytesLong = longToBytes(long1);System.out.println("bytes=" + bytesLong);//bytes=[B@c17164//測試 byte 數組 轉 longlong long2 = bytesToLong(bytesLong);System.out.println("long2=" + long2);//long2=2223}//byte 與 int 的相互轉換public static byte intToByte(int x) {return (byte) x;}public static int byteToInt(byte b) {//Java 總是把 byte 當做有符處理;我們可以通過將其和 0xFF 進行二進位與得到它的無符值return b & 0xFF;}//byte 數組與 int 的相互轉換public static int byteArrayToInt(byte[] b) {    return   b[3] & 0xFF |            (b[2] & 0xFF) << 8 |            (b[1] & 0xFF) << 16 |            (b[0] & 0xFF) << 24;}public static byte[] intToByteArray(int a) {    return new byte[] {        (byte) ((a >> 24) & 0xFF),        (byte) ((a >> 16) & 0xFF),           (byte) ((a >> 8) & 0xFF),           (byte) (a & 0xFF)    };}//byte 數組與 long 的相互轉換    public static byte[] longToBytes(long x) {        buffer.putLong(0, x);        return buffer.array();    }    public static long bytesToLong(byte[] bytes) {        buffer.put(bytes, 0, bytes.length);        buffer.flip();//need flip         return buffer.getLong();    }}

        運行測試結果:
byte0=-22
int1=234
bytesInt=[B@de6ced
int3=1417
bytes=[B@c17164
long2=2223
參考文章1:http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back。
參考文章2:http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java。
參考文章3:http://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.