首页 > 学院 > 开发设计 > 正文

int和byte数组的转换

2019-11-18 11:22:25
字体:
来源:转载
供稿:网友

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



上一篇:Maven Goal !

下一篇:如何设计架构

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表