Skip to content

Instantly share code, notes, and snippets.

@tinwatchman

tinwatchman/byteToString

Last active Aug 29, 2015
Embed
What would you like to do?
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