What is the best way to work around the fact that ALL Java bytes are signed?

后端 未结 7 1178
误落风尘
误落风尘 2020-12-02 12:29

In Java, there is no such thing as an unsigned byte.

Working with some low level code, occasionally you need to work with bytes that have unsigned values greater tha

7条回答
  •  忘掉有多难
    2020-12-02 12:35

    I know this is a very late response, but I came across this thread when trying to do the exact same thing. The issue is simply trying to determine if a Java byte is >127.

    The simple solution is:

    if((val & (byte)0x80) != 0) { ... }
    

    If the real issue is >128 instead, just adding another condition to that if-statement will do the trick.

提交回复
热议问题