I have a uint8 (unsigned 8 bit integer) coming in from a UDP packet. Java only uses signed primitives. How do I parse this data structure correctly with java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Simply read it as as a byte and then convert to an int.
The bitmask is needed, because otherwise, values with bit 8 set to 1 will be converted to a negative int. Example:
So when you afterwards apply the bitmask 0xFF to it, the leading 1’s are getting cancelled out. For your information:
0xFF == 0b11111111