• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to get unsigned bytes  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of what I do deals with unsigned bytes.  Talking to hardware, parsing bitstreams, that sort of thing.  Java's bytes are signed, which is just begging to byte (ha!) me in the ass when I least expect it.  chars are 16 bit unsigned doohickies.  Again, I see lots of places to get tooth marks where I don't want them.

Ideas on the best way to get, and work with, raw 8 bit unsigned bytes in Java?
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really depends on what you need to do with them. For a lot of operations, the "signedness" is just in your head. For instance, the result of addition is the same regardless of whether the bytes are signed or not.

Usually, you can treat bytes as just bytes regardless of their sign, and only when you do something like display their value, you just apply a bitmask that results in an int representing the unsigned value of the byte.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:It really depends on what you need to do with them. For a lot of operations, the "signedness" is just in your head. For instance, the result of addition is the same regardless of whether the bytes are signed or not.

Usually, you can treat bytes as just bytes regardless of their sign, and only when you do something like display their value, you just apply a bitmask that results in an int representing the unsigned value of the byte.


Yeah, again, I've been programming for almost 40 years.  Things like "treat them as just bytes" when your thinking "8 bits, sweet" and the compiler is thinking "8 signed bits, heh heh heh" is a recipe for disaster.

/ not kidding
// bought a TRS-80 in '79.  Learned BASIC
/// Learned Z-80 assembler in '80 or so
//// Got a job as a software engineer in '81 or so.
///// This isn't fark, slashies aren't the best way to get my message across
 
Campbell Ritchie
Marshal
Posts: 56225
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you don't usually use bytes for arithmetic, but for transmission, as Stephan says, you aren't going to notice the difference. Only when you try arithmetic would the JVM (not the compiler) deal with the bytes as signed.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all you need is an 8-bit integer type, then there is no difference between signed and unsigned. The signed value -1 is 11111111 in bits. The unsigned value 255 is also 11111111 in bits.

That's (probably) the number one reason why InputStream.read returns an int and not a byte - there would be no difference between an actual value -1 (11111111) or the end-of-stream marker -1. This was resolved by returning an int where -1 indicates end-of-stream, and values 0 through 255 indicate proper values, which you can cast to a byte without losing any information.

Only if you need to do some arithmetic will you need to make a distinction between signed and unsigned bytes. If that's the case, you can use value & 0xFF to get the same value as an unsigned int.
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Things like "treat them as just bytes" when your thinking "8 bits, sweet" and the compiler is thinking "8 signed bits, heh heh heh" is a recipe for disaster.

Maybe it's best if you gave us an example of something that you want to do, that goes wrong because byte is signed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!