import org.apache.hadoop.hbase.util.Bytes;
public class ArrayTest {
public static void main(String[] args) {
byte[]x=new byte[8];
x[0]=1;
x[1]=2;
x[2]=3;
x[3]=4;
x[4]=5;
x[5]=4;
x[6]=1;
x[7]=127;
// byte[] bs = Arrays.copyOfRange(x, from,2);
Integer short1 = Bytes.toInt(x, 1,4);
System.out.println("'''''''''''''''''''"+short1.byteValue());
byte[] int2Byte = int2Byte(short1);
for (byte b : int2Byte) {
System.out.println("int"+b);
}
Short short2 = Bytes.toShort(x, 1, 2);
// for (byte b : bs) {
// System.out.println(b);
// }
System.out.println("short"+short2.shortValue());
byte[] short2Byte = short2Byte(short2);
for (byte b : short2Byte) {
System.out.println(b);
}
}
public static byte[] short2Byte(short a){
byte[] b = new byte[2];
b[0] = (byte) (a >> 8);
b[1] = (byte) (a);
return b;
}
public static byte[] int2Byte(int a) {
byte[] b = new byte[4];
b[0] = (byte) (a >> 24);
b[1] = (byte) (a >> 16);
b[2] = (byte) (a >> 8);
b[3] = (byte) (a);
return b;
} }
结果如下
”””””””””’5
int2
int3
int4
int5
short515
2
3