I have a array of bytes.
bytes[] = [43, 0, 0, -13, 114, -75, -2, 2, 20, 0, 0]
I want to convert it to unsigned bytes in Java. this is what I did: created a new array and copy the values with & 0xFF:
this.bytes = new byte[bytes.length];
for (int i=0;i<bytes.length;i++)
this.bytes[i] = (byte) (bytes[i] & 0xFF);
but the values stay negative in the new array as well. what am I doing wrong?
byteValue & 0xFFto do?int. If you just cast a byte with a negative value e.g. into anintyou end up with anintwith that negative number. So that part of the code wasn't the problem, just the one where it's put back (i.e. casted again) into abyteending up with the same negative value you started with.