您的位置:首页 > 其它

int和byte数组的转换

2004-11-17 15:29 330 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: