1. BYTE类型变量赋值注意事项:
如下定义:
byte [] t1 = new byte[5];
赋值:
byte[0] = 0x05;
byte[1] = 0x7f;
byte[2] = 0x80;
byte[3] = 0xa0;
byte[4] = 0xff;
如上赋值,byte[2] ~ byte[3]会报错:type mismatch: cannot convert from int to byte.
注意:不像 C/C++,Java 不支持无符号类型(unsigned). byte [2]=0x80报错是因为这样等同于byte [2]= (int)0x80即byte b= 128,这样就超出了byte的取值范围,所以会报错。byte[3]/byte[4]相同。
正确赋值方式如下:
byte[0] = (byte) 0x05;
byte[1] = (byte) 0x7f;
byte[2] = (byte) 0x80;
byte[3] = (byte) 0xa0;
byte[4] = (byte) 0xff;