Convert from Decimal to Binary, Octal or Hex using java
Use Integer.toString(int input, int radix) to convert from an Integer to any type of base number.
Integer decimal1 =
21
;
String binaryNumber = Integer.toString(decimal1,
2
);
System.out.println(decimal1 +
" in Base 2 : "
+ binaryNumber);
Integer decimal2 =
302
;
String octalNumber = Integer.toString(decimal2,
8
);
System.out.println(decimal2 +
" in Base 8 : "
+ octalNumber);
Integer decimal3 =
43981
;
String hexNumber = Integer.toString(decimal3,
16
);
System.out.println(decimal2 +
" in Base 16 : "
+ hexNumber);
Output:
21
in Base
2
:
10101
302
in Base
8
:
456
43981
in Base
16
: abcd
Comments
Post a Comment