Created
June 6, 2020 07:52
-
-
Save tasdemirbahadir/90fe111892050626416c17a79a68febc to your computer and use it in GitHub Desktop.
Convert a string representation of a number to binary format. For example "2" -> "00000010"
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
private static String numToBin(String num) { | |
BigInteger bi = new BigInteger(num); | |
byte[] val = bi.toByteArray(); | |
StringBuilder sb = new StringBuilder(); | |
for (byte b: val) { | |
sb.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0')); | |
} | |
sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment