buffer is a bytebuffer .I'm getting a lost of percision error with this.
byte myPort = buffer.get(0); // Might need to change this depending on byte order
switch(myPort){
case 0xF1: // Chat service
break;
case 0xF2: // Voice service
break;
case 0xF3: // Video service
break;
case 0xF4: // File transfer service
break;
case 0xF5: // Remote login
break;
}
Apparently, 0xFF is not a byte in java and its really confusing me. I don't know if I'm losing it but isn't 0xF a nibble and 0xFF a byte? Apparently my ide netbeans allows byte values all the way up to 127. This seems to be a problem with signed values, but I'm not sure why.
Thanks for any help.
As already stated in the comments, byte is [-128 .. 127].
You should convert your byte from the byte buffer to int, to preserve the "unsigned byte" range you assume (and which does not exist in Java):
int myPort = buffer.get(0) & 0xff; // Might need to change this depending on byte order
switch(myPort){
case 0xF1: // Chat service
break;
case 0xF2: // Voice service
break;
case 0xF3: // Video service
break;
case 0xF4: // File transfer service
break;
case 0xF5: // Remote login
break;
}
Note that simply assigning the byte to an int would not work due to sign extension:
byte val = (byte) 0xb6;
int myPort = val;
results in myPort = -74 (0xffffffb6), while
byte val = (byte) 0xb6;
int myPort = val & 0xff;
results in myPort = 182 (0xb6).
See Why doesn't Java support unsigned ints? for some good background information on the absence of unsigned data types in Java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With