801 WORKPLACE

追寻梦想,自由生活

BlogJava 首页 新随笔 联系 聚合 管理
  10 Posts :: 1 Stories :: 1 Comments :: 0 Trackbacks

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

posted on 2010-11-30 22:18 WangShishuai 阅读(343) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: