Getting unsigned bytes in java

Welcome back after long overwork and vacation period :) Let’s start small, it’s still almost summer.

Bytes in java are always signed and are quite horrible to use because of the casting and weird side-effects (or I’m just stupid enough to call it weird).

But we need unsigned bytes quite a lot. Most of the time for reading various streams (networks, 8bit strings etc) some text stream you still end up with byte streams.

There is a nice trick converting them from signed to unsigned in case you want to get normal 0..255 range for the chars.

byte mybyte = -104;
long mynewint = mybyte & 0xFF;

Hope it helps!

I had quite a nice puzzling while trying to find some easier way. Is there?

Share:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit
Comments (4) Friday, August 15, 12:15 am