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;
}
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;
}
相关文章推荐
- 由C#风潮想起的——给初学编程者的忠告
- 基于Web的系统测试方法
- 20世纪末的大学生活(一)
- Sun将免费提供Solaris 10
- Sun将免费提供Solaris 10
- 关于phpMyAdmin中config的配置说明
- ACCESS转换工具
- ASP.NET开发闲谈
- 关于触发器的问题?
- Nokia 60的播放Tone声音被按键中断问题
- Adobe正式发布Acrobat 7
- 天依旧是这么灰蒙蒙的
- Adobe正式发布Acrobat 7
- java新闻:新一代Java技术即将出现(z)
- 给DBGrid添加鼠标滚动事件
- 应用Rational 工具简化基于J2EE的项目
- SQL优化
- 我被Build撞了一下腰
- Red Hat下WEB服务器的配置
- 综合型教育网BBS网站安全浅议 以YTHT为例