wierob [] β int []!
, .
1) API
public static int[] punnedToInteger(byte[] in){
ByteBuffer bb = ByteBuffer.wrap(in);
IntBuffer pb = bb.asIntBuffer();
int[] out = new int[pb.limit()];
pb.get(out);
return out;
}
public static byte[] punnedFromInteger(int[] in){
byte[] out = new byte[in.length * Integer.SIZE / Byte.SIZE];
ByteBuffer bb = ByteBuffer.wrap(out);
for(int i=0; i<in.length; ++i){
bb.putInt(in[i]);
}
return out;
}
2)
{
byte[] bytes = new byte[]{ 0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0 };
int[] ints = punnedToInteger(bytes);
System.out.println(Arrays.toString(bytes));
System.out.println(Arrays.toString(ints));
System.out.println();
}
{
int[] ints = new int[]{ 1, 256, 65536, 16777216 };
byte[] bytes = punnedFromInteger(ints);
System.out.println(Arrays.toString(ints));
System.out.println(Arrays.toString(bytes));
System.out.println();
}
3)
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0]
[1, 256, 65536, 16777216]
[1, 256, 65536, 16777216]
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0]