java中的int类型和byte数组的相互转换

来源:互联网 发布:关于弹钢琴的软件 编辑:程序博客网 时间:2024/05/19 16:47
在java中int占4个字节,byte占1个字节。

public static byte[] intToByte(int N) {byte[] a = new byte[4];for(int i=a.length-1, n=N; i>=0; --i){  a[i] = (byte)(n & 0xff);  n >>= 8;}//for(int i=0;i<a.length;i++) {//System.out.printf(" %02x ",a[i]);//}return a;}public static int byteToInt2(byte[] b) {        int mask=0xff;        int temp=0;        int n=0;        for(int i=0;i<4;i++){           n<<=8;           temp=b[i]&mask;           n|=temp;       }return n;}
原创粉丝点击