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
Post a Comment