在Java中,有时候我们需要在有无符号数之间转换。
byte 有符号转换无符号:
byte b1 = -36; System.out.println(b1&0xff);
byte 无符号转换有符号:
// 方法一
byte b2 = (byte)276;
System.out.println(b2);
// 方法二
BigInteger b= new BigInteger("276");
byte b3= b.byteValue();
System.out.println(b3);说明:
如果只需要对英文文本的每个字节进行数据处理,则无需考虑有符号数和无符号数的转换问题;
但如果需要对含有中文的文本进行字节处理,则可能需要考虑有无符号数的转换问题。
评论