Compare unsigned bytes in Java

Convert to int and compare.

int cmp = Integer.compare(Byte.toUnsignedInt(b1), Byte.toUnsignedInt(b2))
// cmp = -1  =>  b1 < b2
// cmp =  0  =>  b1 = b2
// cmp =  1  =>  b1 > b2

Or, using Guava

int cmp = UnsignedBytes.compare(b1, b2);

Comments

Be the first to comment!