首页 > 代码库 > Java - byte[]与int的互相转换

Java - byte[]与int的互相转换

 

public static int byte2Int(byte[] b, int begin, int len) {    int sum = 0;    int end = begin + len;    int n;    for (int i = begin; i < end; i++) {        n = ((int) b[i]) & 0xff;        n <<= (--len) * 8;        sum = n + sum;    }    return sum;}public static byte[] int2byte(int val, int len) {    byte[] b = new byte[len];    for (int i = 0; i < len; i++) {        b[len - i - 1] = (byte) ((val >> 8 * i) & 0xff);    }    return b;}

 

Java - byte[]与int的互相转换