java int 0x_Java int 与 byte的转换 & 0xFF

Java int 与 byte的转换 & 0xFF

int -> byte

采用强制类型转换 byte 类型的取值范围是 -128~127。当把int转换成byte时,超出这个范围,值就不会相等。

int ii = 128;

byte bb = (byte) ii;

System.out.println(ii == bb); //false

int iii = 127;

byte bbb = (byte) iii;

System.out.println(iii == bbb);//true

通过InputStream的read()方法获取的int,可采用强制类型转换将值还原为byte类型,

/**

* Reads the next byte of data from the input stream. The value byte is

* returned as an int in the range 0 to

* 255. If no byte is available because the end of the stream

* has been reached, the value -1 is returned. This method

* blocks until input data is available, the end of the stream is detected,

* or an exception is thrown.

*

*

A subclass must provide an implementation of this method.

*

* @return the next byte of data, or -1 if the end of the

* stream is reached.

* @exception IOException if an I/O error occurs.

*/

public abstract int read() throws IOException;

注意这个int 值的范围是 0~255,转换成byte,不会超出byte的位的长度。

byte -> int

这里有两种情况,一种是要求保持值不变,例如进行数值计算,可采用强制类型转换:int i = (int) aByte; 另一种是要求保持最低字节中各个位不变,3个高字节全部用0填充,例如进行编解码操作, 则需要采用位操作:int i = b & 0xff;

通过ByteArrayInputStream的read()方法,通过位操作将byte转换为int,

/**

* Reads the next byte of data from this input stream. The value

* byte is returned as an int in the range

* 0 to 255. If no byte is available

* because the end of the stream has been reached, the value

* -1 is returned.

*

* This read method

* cannot block.

*

* @return the next byte of data, or -1 if the end of the

* stream has been reached.

*/

public synchronized int read() {

return (pos < count) ? (buf[pos++] & 0xff) : -1;

}

int byte[]

public class Transfer {

public static byte[] toByteArray(int iSource, int iArrayLen) {

byte[] bLocalArr = new byte[iArrayLen];

for (int i = 0; (i < 4) && (i < iArrayLen); i++) {

bLocalArr[i] = (byte) (0xFF & (iSource >>> (i << 3)));

}

return bLocalArr;

}

public static int toInt(byte[] bRefArr) {

int iOutcome = 0;

byte bLoop;

for (int i = 0; i < bRefArr.length; i++) {

bLoop = bRefArr[i];

iOutcome += (bLoop & 0xFF) << (i << 3);

}

return iOutcome;

}

public static void main(String args[]) {

int a = 100;

byte[] arr = toByteArray(a, 4);

int b = toInt(arr);

System.out.println(b);

}

}

为什么要和 0xFF 进行与操作

以这个为例,

public static byte[] toByteArray(int iSource, int iArrayLen) {

byte[] bLocalArr = new byte[iArrayLen];

for (int i = 0; (i < 4) && (i < iArrayLen); i++) {

bLocalArr[i] = (byte) (0xFF & (iSource >>> (i << 3)));

}

return bLocalArr;

}

int类型在java中用8个字节32位来表示,byte类型为一个字节8位。我们要做的就是将32位分四次,每次获取8位写入到字节数组中。首先是低8位,和0xFF相与后,高24比特就会被置为0了,写入到字节数组中。然后获取后边的8位,首先要移位,无符号右移8位,和0xFF相与后,保留8位,把这八位写入到数组中。后边依次类推。最终完整的把int写入到字节数组中。

=========END=========

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值