龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

int和byte数组的转换

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:

  int转换成长度为4的byte数组,长度为4的byte数组合成一个int.
  

static int bytes2int(byte[] b)
  {
           //byte[] b=new byte[]{1,2,3,4};
           int mask=0xff;
           int temp=0;
          int res=0;
          for(int i=0;i<4;i++){
              res<<=8;
              temp=b[i]&mask;
              res=temp;
          }
         return res;
  }

  

  static byte[] int2bytes(int num)
  {
         byte[] b=new byte[4];
         int mask=0xff;
         for(int i=0;i<4;i++){
              b[i]=(byte)(num>>>(24-i*8));
         }
        return b;
  }


  
精彩图集

赞助商链接