views:

2412

answers:

6

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 than 128, which causes Java to interpret them as a negative number due to the MSB being used for sign.

What's a good way to work around this? (Saying don't use Java is not an option)

A: 

I guess you could just use a short to store them. Not very efficient, but really the only option besides some herculean effort that I have seen.

Nick Berardi
A: 

Probably your best bet is to use an integer rather than a byte. It has the room to allow for numbers greater than 128 without the overhead of having to create a special object to replace byte.

This is also suggested by people smarter than me (everybody)

stimms
+1  A: 

When reading any single value from the array copy it into something like a short or an int and manually convert the negative number into the positive value it should be.

byte[] foobar = ..;
int value = foobar[10];
if (value < 0) value += 256 // Patch up the 'falsely' negative value

You can do a similar conversion when writing into the array.

pauldoo
Much easier and more efficient to do int value=(foobar[10]
Software Monkey
pauldoo
A: 

The best way to do bit manipulation/unsigned bytes is through using ints. Even though they are signed they have plenty of spare bits (32 total) to treat as an unsigned byte. Also, all of the mathematical operators will convert smaller fixed precision numbers to int. Example:

short a = 1s;
short b = 2s;
int c = a + b; // the result is up-converted
short small = (short)c; // must cast to get it back to short

Because of this it is best to just stick with integer and mask it to get the bits that you are interested in. Example:

int a = 32;
int b = 128;
int foo = (a + b) | 255;

Here is some more info on Java primitive types http://mindprod.com/jgloss/primitive.html

One last trivial note, there is one unsigned fixed precision number in Java. That is the char primitive.

martinatime
A: 

Using ints is generally better than using shorts because java uses 32-bit values internally anyway (Even for bytes, unless in an array) so using ints will avoid unnecessary conversion to/from short values in the bytecode.

izb
+19  A: 

@pauldoo

It is actually possible to get rid of the if statement and the addition if you do it like this.

byte[] foobar = ..;
int value = (foobar[10] & 0xff);

This way Java doesn't interpret the byte as a negative number and flip the sign bit on the integer also.

ejack