Peter Mel
Peter Mel

Reputation: 367

What happened to the byte 0xFF (11111111) in Java?

I need to send the byte 0xFF to a client, but when I do this:

byte toSend = 0b11111111;
// or
byte toSend = 0xFF;

I get a compile error sending it can't convert from int to byte.

I want to make sure I am sending a byte with all 8 bits set to 1. How can I do that in Java?

Upvotes: 1

Views: 614

Answers (1)

Eran
Eran

Reputation: 393936

You can cast it to byte :

byte toSend = (byte)0xFF;

Forget the other part of my answer. I'm a bit sleep deprived. Actually it's -1 that is represented as 11111111. But (byte)0xFF works just fine.

Upvotes: 2

Related Questions