求救:byte型的数组和int是如何进行相互转换的? 首先 Head[20]数组越界,会有一个系统异常如果你想把最后一个字节给int的话,直接 totalLength=Head[19] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我敲错了,不是Head[20],是把Head这整个数组转换成int. 是每一个byte转换为一个Integer还是4个byte转换为一个Integer? 是4个byte转换为一个,而且有一步还需要这样做byte tempbytes[] = (new byte[] { Head[4], Head[5], Head[6], Head[7] });然后把tempbytes转换为一个int 对呀,是转,假如是使用四个byte转化为一个int,那么使用byteBuffer,然后在getInt就可以了,会自动将4个字节的数据转化为一个int 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;}System.out.println(Integer.toHexString(res)); shangqiao,用byteBuffer,然后在getInt好象不行啊treeroot(根根),能不能说说你这样做的原因. 因为java中都是有符号数,所以需要屏蔽 int转换成长度为4的byte数组,长度为4的byte数组合成一个int.short和long类似.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;这里可以不用mask for(int i=0;i<4;i++){ b[i]=(byte)(num>>>(24-i*8)); } return b;} equals和== 这段代码怎么运行没有结果啊!! jdbc连接? JAVA怎么取客户端的网卡号 Frame 怎么不显示最大化和最小化那两个按钮 Jmagick的问题 java几个字符串查找出相同的字符串,在线等.. 如何学??? 惭愧,我装了java sdk2.0,却不知道如何用? 如何生存一串数字曲线数字 怎样利用StringTokenizer读负数?(俺只会读正数)郁闷 有没有能在手机上看的thinking in java啊
byte tempbytes[] = (new byte[] {
Head[4], Head[5], Head[6], Head[7]
});
然后把tempbytes转换为一个int
int mask=0xff;
int temp=0;
int res=0;
for(int i=0;i<4;i++){
res<<=8;
temp=b[i]&mask;
res|=temp;
}
System.out.println(Integer.toHexString(res));
treeroot(根根),能不能说说你这样做的原因.
short和long类似.
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;这里可以不用mask
for(int i=0;i<4;i++){
b[i]=(byte)(num>>>(24-i*8));
}
return b;
}