`

byte int 转换

    博客分类:
  • java
阅读更多
/**
 * 将byte类型数据转为int型
 */
public static int bytesToInt(byte[] bytes) {
  int mask = 0xff, temp = 0, result = 0;
  for (int i = 0; i < 4; i++) {
   result <<= 8;
   temp = bytes[i] & mask;
   result |= temp;
  }
  return result;
}

/**
 * 将int类型数据转为byte型
 */
public static byte[] intToBytes(int num){
  byte[] result = new byte[4];
  for(int i=0;i<4;i++){
   result[i] = (byte)(num>>>(24-i*8));
  }
  return result;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics