java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to convert BigInteger into another radix number?

In this example you'll see how we can convert a BigInteger number into another radix such as binary, octal and hexadecimal.

package com.javacoderanch.example.math;

import java.math.BigInteger;

public class BigIntegerConversion {
    public static void main(String[] args) {
        BigInteger number = new BigInteger("2008");

        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      = 2008
Binary      = 11111011000
Octal       = 3730
Hexadecimal = 7d8
Number      = 255
Number      = ff