Hexadecimal enumeration types use java to return wrong values?
•
Java
I use the following enumeration types in the following code:
public static enum PanelType { PAS8((byte)0xA6),PAS83((byte)0xA7); private byte code; private PanelType(byte code) { this.code=code; } public byte getCode(){ return code; } }
However, when I try to use it in my method:
for (PanelType type:PanelType.values()) { if (decoded[3]==type.getCode()) return type; }
I will return: type Error value of getcode () method It returns - 90 instead of 166, which is exactly what I expected
I know ffff ffff ffff ffa6 = - 90, but why does 0xa6 return as a negative number?
Solution
The maximum value of bytes is 127 and the minimum value is - 128 0xa6 is decimal 166, so there is an overflow:
-128 + (166 - 127 - 1) == -90
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码