byte[]转换为int
int转换为byte[]
- public int bytesToInt(byte[] data) {
- if (data.length != 4) {
- throw new IllegalArgumentException();
- }
- ByteBuffer byteBuffer = ByteBuffer.allocate(4);
- byteBuffer.put(data, 0, 4);
- byteBuffer.flip();
- return byteBuffer.getInt();
- }
- public byte[] intToBytes(int i) {
- ByteBuffer byteBuffer = ByteBuffer.allocate(4);
- byteBuffer.putInt(i);
- byteBuffer.flip();
- return byteBuffer.array();
- }