In this example you’ll see how we can convert a java.math.BigInteger
number into another radix such as binary, octal and hexadecimal.
package org.kodejava.math;
import java.math.BigInteger;
public class BigIntegerConversion {
public static void main(String[] args) {
BigInteger number = new BigInteger("2021");
System.out.println("Number = " + number);
System.out.println("Binary = " + number.toString(2));
System.out.println("Octal = " + number.toString(8));
System.out.println("Hexadecimal = " + number.toString(16));
number = new BigInteger("FF", 16);
System.out.println("Number = " + number);
System.out.println("Number = " + number.toString(16));
}
}
The result of our examples:
Number = 2021
Binary = 11111100101
Octal = 3745
Hexadecimal = 7e5
Number = 255
Number = ff
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024