Last active
August 29, 2015 14:06
-
-
Save tinwatchman/4de7d80663732a0fe47a to your computer and use it in GitHub Desktop.
Java function - convert a byte into a binary string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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