byte[]与int

来源:互联网 发布:excel查找同列重复数据 编辑:程序博客网 时间:2024/05/15 23:52
byte[]转换为int
  1. public int bytesToInt(byte[] data) {
  2.     if (data.length != 4) {
  3.         throw new IllegalArgumentException();
  4.     }
  5.     ByteBuffer byteBuffer = ByteBuffer.allocate(4);
  6.     byteBuffer.put(data, 04);
  7.     byteBuffer.flip();
  8.     return byteBuffer.getInt();
  9. }
int转换为byte[]
  1. public byte[] intToBytes(int i) {
  2.     ByteBuffer byteBuffer = ByteBuffer.allocate(4);
  3.     byteBuffer.putInt(i);
  4.     byteBuffer.flip();
  5.     return byteBuffer.array();      
  6. }


原创粉丝点击