So, here's how it works.
You can treat Java bytes as unsigned. The only places where signs make a difference are
- constants: just cast them to bytes
- toString and parseInt
- division
- <, >, >=, <=
Operations where signedness does not matter:
- addition
- subtraction
- multiplication
- bit arithmetic (except for
>>
, just use>>>
instead)
To convert bytes to their unsigned values as ints, just use & 0xFF
, and to convert those to bytes use (byte)
.
Alternatively, if third-party libraries are acceptable, you might be interested in Guava's UnsignedBytes
utility class. (Disclosure: I contribute to Guava.)