Skip to content

Instantly share code, notes, and snippets.

@NicMcPhee
Created March 31, 2015 23:53
Show Gist options
  • Save NicMcPhee/49d7a597e8d8c1e25ab8 to your computer and use it in GitHub Desktop.
Save NicMcPhee/49d7a597e8d8c1e25ab8 to your computer and use it in GitHub Desktop.
A little example showing how to convert Java's signed bytes to the appropriate unsigned integer value.
public class ConvertingSignedBytes {
public static void main(String[] args) {
byte x = (byte) 37;
byte y = (byte) 150;
printByte(x);
printByte(y);
}
public static void printByte(byte b) {
int value;
if (b < 0) {
value = 256 + b;
} else {
value = b;
}
System.out.println(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment