Convert from Binary, Octal or Hex to Decimal number in java

Use Integer.parseInt(String input, int radix) to convert from any type of number to an Integer.

String binaryNumber = "10101";
int decimal1 = Integer.parseInt(binaryNumber, 2);
System.out.println(binaryNumber + " in Base 10 : " + decimal1);
String octalNumber = "456";
int decimal2 = Integer.parseInt(octalNumber, 8);
System.out.println(octalNumber + " in Base 10 : " + decimal2);
String hexNumber = "ABCD";
int decimal3 = Integer.parseInt(hexNumber, 16);
System.out.println(hexNumber + " in Base 10 : " + decimal3);
Output:
10101 in Base 10 : 21
456 in Base 10 : 302
ABCD in Base 10 : 43981


Comments

Popular posts from this blog

How to Install AnyDesk remote desktop client on Ubuntu

How to install Jaspersoft Studio on Eclipse

What is Advanced Encryption Standard (AEC) and online tool to encrypt and decrypt data using AEC.