服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

int和byte数组的转换

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;
}

扫描关注微信公众号