Skip to content

Instantly share code, notes, and snippets.

@tinwatchman
Last active August 29, 2015 14:06
Show Gist options
  • Save tinwatchman/4de7d80663732a0fe47a to your computer and use it in GitHub Desktop.
Save tinwatchman/4de7d80663732a0fe47a to your computer and use it in GitHub Desktop.
Java function - convert a byte into a binary string
public static String byteToString(byte b) {
byte[] masks = { -128, 64, 32, 16, 8, 4, 2, 1 };
StringBuilder builder = new StringBuilder();
for (byte m : masks) {
if ((b & m) == m) {
builder.append('1');
} else {
builder.append('0');
}
}
return builder.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment