050_整形和字节数组转换

1. 整形和字节数组转换公式

int a = -128
byte[] b = {(byte)(a >> 24), (byte)(a >> 16) , (byte)(a >> 8) , (byte)a}
b[0] = (byte)(a >> 24)
b[0] = (byte)(a >> 16)
b[0] = (byte)(a >> 8)
b[0] = (byte)a 
int result = ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) | ((b[2] & 0xFF) << 8) | (b[3] & 0xFF) = (((byte)(a >> 24) & 0xFF) << 24) | (((byte)(a >> 16) & 0xFF) << 16) | (((byte)(a >> 8) & 0xFF) << 8) | (a & 0xFF)

2. 整形和字节数组的互相转换例子

/**
 * 整形和字节数组的互相转换, 当成公式记
 */
public class ByteArrayIntChangeStyle {
	public static void main(String[] args) {
		for(int i = Byte.MIN_VALUE * 50; i <= Byte.MAX_VALUE; i++) {
			System.out.println(i + " " + byteArrayToInt(intToByteArray(i)));
		}
	}
	
	// byte类型的数值与 0xFF按位与, 使得不关心的位全是0
public static int byteArrayToInt(byte[] b) {  
    	return  (b[0] & 0xFF) << 24 |  
    			(b[1] & 0xFF) << 16 |  
            (b[2] & 0xFF) << 8 | 
            b[3] & 0xFF;
}  
  
// 强制转换成了byte类型, 丢弃了高位, 所以与 0xFF按位与操作可有可无
public static byte[] intToByteArray(int a) {  
    	return new byte[] {  
//	        (byte) ((a >> 24) & 0xFF),  
//	        (byte) ((a >> 16) & 0xFF),     
//	        (byte) ((a >> 8) & 0xFF),     
//	        (byte) (a & 0xFF)
    		(byte) (a >> 24),  
	        (byte) (a >> 16),     
	        (byte) (a >> 8),     
	        	(byte) a
    	};  
}
	
}

3. 分析整形-1和字节数组之间的互相转换

-1的原码: 10000000 00000000 00000000 00000001

-1的反码: 11111111 11111111 11111111 11111110

-1的补码: 11111111 11111111 11111111 11111111

字节数组byte[] b = new byte[4];

3.1. b[0] = (byte) ((-1 >> 24) & 0xFF) , 存储的是-1的25-32位的数据

-1的右移24位, 高位用1填补:      11111111 11111111 11111111 11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

-1和0xFF按位与:                00000000 00000000 00000000 11111111

-1和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:             11111111

-1和0xFF按位与, 转换成byte类型, 反码:                       11111110

-1和0xFF按位与, 转换成byte类型, 原码:                       10000001

结果: -1

3.2. b[1] = (byte) ((-1 >> 16) & 0xFF) , 存储的是-1的17-24位的数据

-1的右移16位, 高位用1填补:      11111111 11111111 11111111 11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

-1和0xFF按位与:                00000000 00000000 00000000 11111111

-1和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:             11111111

-1和0xFF按位与, 转换成byte类型, 反码:                       11111110

-1和0xFF按位与, 转换成byte类型, 原码:                       10000001

结果: -1

 

3.3. b[2] = (byte) ((-1 >> 8) & 0xFF), 存储的是-1的9-16位的数据

-1的右移8位, 高位用1填补:      11111111 11111111 11111111 11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

-1和0xFF按位与:                00000000 00000000 00000000 11111111

-1和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:             11111111

-1和0xFF按位与, 转换成byte类型, 反码:                       11111110

-1和0xFF按位与, 转换成byte类型, 原码:                       10000001

结果: -1

3.4. b[3] = (byte) (-1 & 0xFF) , 存储的是-1的1-8位的数据

-1的补码:                       11111111 11111111 11111111 11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

-1和0xFF按位与:                00000000 00000000 00000000 11111111

-1和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:             11111111

-1和0xFF按位与, 转换成byte类型, 反码:                       11111110

-1和0xFF按位与, 转换成byte类型, 原码:                       10000001

结果: -1

3.5. b = {-1, -1, -1, -1}

3.6. (b[0] & 0xFF) << 24

b[0]的补码:                                                  11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

b[0]和0xFF按位与:               00000000 00000000 00000000 11111111

b[0]和0xFF按位与, 左移24位, 低位0填补:

    11111111 00000000 00000000 00000000

3.7. (b[1] & 0xFF) << 16

b[1]的补码:                                                  11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

b[1]和0xFF按位与:               00000000 00000000 00000000 11111111

b[1]和0xFF按位与, 左移16位, 低位0填补:

    00000000 11111111 00000000 00000000

3.8. (b[2] & 0xFF) << 8

b[2]的补码:                                                  11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

b[2]和0xFF按位与:               00000000 00000000 00000000 11111111

b[2]和0xFF按位与, 左移8位, 低位0填补:

    00000000 00000000 11111111 00000000

3.9. b[3] & 0xFF

b[3]的补码:                                                  11111111

0xFF的补码:                     00000000 00000000 00000000 11111111

b[3]和0xFF按位与:               00000000 00000000 00000000 11111111

3.10. b[0] | b[1] | b[2] | b[3]

b[0]补码:                        11111111 00000000 00000000 00000000

b[1]补码:                        00000000 11111111 00000000 00000000

b[2]补码:                        00000000 00000000 11111111 00000000

b[3]补码:                        00000000 00000000 00000000 11111111

b[0] b[1] b[2] b[3]按位或操作:      11111111 11111111 11111111 11111111

b[0] b[1] b[2] b[3]按位或操作反码:  11111111 11111111 11111111 11111110

b[0] b[1] b[2] b[3]按位或操作原码:  10000000 00000000 00000000 00000001

结果: -1

4. 分析整形128和字节数组之间的互相转换

128的补码: 00000000 00000000 00000000 10000000

字节数组byte[] b = new byte[4];

4.1. b[0] = (byte) ((128 >> 24) & 0xFF) , 存储的是128的25-32位的数据

128的右移24位, 高位用0填补:    00000000 00000000 00000000 00000000

0xFF的补码:                     00000000 00000000 00000000 11111111

128和0xFF按位与:               00000000 00000000 00000000 00000000

128和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:           00000000

结果: 0

4.2. b[1] = (byte) ((128 >> 16) & 0xFF) , 存储的是128的17-24位的数据

128的右移16位, 高位用0填补:    00000000 00000000 00000000 00000000

0xFF的补码:                     00000000 00000000 00000000 11111111

128和0xFF按位与:               00000000 00000000 00000000 00000000

128和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:           00000000

结果: 0

4.3. b[2] = (byte) ((128 >> 8) & 0xFF), 存储的是128的9-16位的数据

128的右移8位, 高位用0填补:    00000000 00000000 00000000 00000000

0xFF的补码:                     00000000 00000000 00000000 11111111

128和0xFF按位与:               00000000 00000000 00000000 00000000

128和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:           00000000

结果: 0

4.4. b[3] = (byte) (128 & 0xFF) , 存储的是128的1-8位的数据

128的补码:                      00000000 00000000 00000000 10000000

0xFF的补码:                     00000000 00000000 00000000 11111111

128和0xFF按位与:               00000000 00000000 00000000 10000000

128和0xFF按位与, 转换成byte类型, 丢弃掉高位数据:           10000000

结果: -128

4.5. b = {0, 0, 0, -128}

4.6. (b[0] & 0xFF) << 24

b[0]的补码:                                                  00000000

0xFF的补码:                     00000000 00000000 00000000 11111111

b[0]和0xFF按位与:               00000000 00000000 00000000 00000000

b[0]和0xFF按位与, 左移24位, 低位0填补:

    00000000 00000000 00000000 00000000

4.7. (b[1] & 0xFF) << 16

b[1]的补码:                                                  00000000

0xFF的补码:                     00000000 00000000 00000000 11111111

b[1]和0xFF按位与:               00000000 00000000 00000000 00000000

b[1]和0xFF按位与, 左移16位, 低位0填补:

    00000000 00000000 00000000 00000000

4.8. (b[2] & 0xFF) << 8

b[2]的补码:                                                  00000000

0xFF的补码:                     00000000 00000000 00000000 11111111

b[2]和0xFF按位与:               00000000 00000000 00000000 00000000

b[2]和0xFF按位与, 左移8位, 低位0填补:

    00000000 00000000 00000000 00000000

4.9. b[3] & 0xFF

b[3]的补码:                                                  10000000

0xFF的补码:                     00000000 00000000 00000000 11111111

b[3]和0xFF按位与:               00000000 00000000 00000000 10000000

4.10. b[0] | b[1] | b[2] | b[3]

b[0]补码:                        00000000 00000000 00000000 00000000

b[1]补码:                        00000000 00000000 00000000 00000000

b[2]补码:                        00000000 00000000 00000000 00000000

b[3]补码:                        00000000 00000000 00000000 10000000

b[0] b[1] b[2] b[3]按位或操作:      00000000 00000000 00000000 10000000

结果: 128

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值