Skip to content

Instantly share code, notes, and snippets.

@tasdemirbahadir
Created June 6, 2020 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tasdemirbahadir/90fe111892050626416c17a79a68febc to your computer and use it in GitHub Desktop.
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"
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