views:

64

answers:

2

alright, so my code to read bytes into a int is like so:

int offset = (byte << 16) | (byte2  << 8) | byte3;

And it's reading the bytes "00 00 be" as -66.

How do I read it as the 190 it's meant to be?

+1  A: 
    byte b = -66;
    int i = b < 0 ? b + 256 : b;

It might be useful declare helper function for this.

Nikita Rybak
+4  A: 
byte b = -66;
int i = b & 0xff;
Jason S
that worked. I'll be accepting this answer as soon as I can ^_^ thank you.
William
an explanation perhaps: this works because the `0xff` literal is an int, not a byte. Otherwise the bitwise AND with `0xff` would yield the same byte again.
Wim Coenen
Jason S