想把一个数分别存在两个byte数组中,但好像有适用范围。比如我存得0x3c就能实现正确的转换,但我存得是0xff怎么变成-1了。求解释
class Test {
byte unit1;
byte unit2;
public static void main(String[] args) {
int counter=0x3c;
Test a=new Test();
a.setRecvCounter(counter);
System.out.println("counter = "+counter);
System.out.println("a.getRecvCounter() = "+a.getRecvCounter());
}
private int getRecvCounter() {
return (unit1 >> 1) + (unit2 << 8);
}
private void setRecvCounter(int recvcounter) {
unit1 = (byte) ((recvcounter << 1) & 0xff);
unit2 = (byte) ((recvcounter >> 8) & 0xff);
}
}
|
|