int类型byte互转 如何将int转换成byte?
如何将int转换成byte?因为在Java中,int type占用4个字节,而byte占用1个字节,所以当int type转换为byte type时会发生位丢失,即int的低8位用作byte type
如何将int转换成byte?
因为在Java中,int type占用4个字节,而byte占用1个字节,所以当int type转换为byte type时会发生位丢失,即int的低8位用作byte type的值。int变量的值是257,对应的二进制是10000001,最后8位是00000001。第一个0表示符号位并表示一个正数,因此变量x的值是1。
如何把一个byte数组的数字转换成int?
Int to byte array public static byte[]inttobytes 2(INTN){byte[]B=newbyte[4]for(inti=0I<4i){B[i]=(byte)(n>>(24-i*8))}return}byte to intPUBLIC static byte to INT2(byte[]B){intmask=0xffinttemp=0intn=0For(inti=0I)<b.lengthi公司){n<<=8temp=b[i]&maskn |=temp}返回n}