ITeye博客

byte int 转换

java.zhangshuling · 2011-07-07 14:51 · java · 编程语言 · Markdown


/**
 * 将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;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。