Skip to content

Instantly share code, notes, and snippets.

@ellenia
Created October 2, 2017 03:12
Show Gist options
  • Save ellenia/d91e5594f17d9a36d94cb1efb2840275 to your computer and use it in GitHub Desktop.
Save ellenia/d91e5594f17d9a36d94cb1efb2840275 to your computer and use it in GitHub Desktop.
//first way
//decimal to binary
String binaryString = Integer.toBinaryString(number);
System.out.println("decimal to binary: " + binaryString);
//decimal to octal
String octalString = Integer.toOctalString(number);
System.out.println("decimal to octal: " + octalString);
//decimal to hexadecimal
String hexString = Integer.toHexString(number);
System.out.println("decimal to hexadecimal: " + hexString);
//second way
binaryString = Integer.toString(number,2);
System.out.println("decimal to binary using Integer.toString: " + binaryString);
//decimal to octal
octalString = Integer.toString(number,8);
System.out.println("decimal to octal using Integer.toString: " + octalString);
//decimal to hexadecimal
hexString = Integer.toString(number,16);
System.out.println("decimal to hexadecimal using Integer.toString: " + hexString);
//javarevisited.blogspot.com/2011/10/convert-decimal-binary-octal-java.html#ixzz4uJhRMI1l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment