Base conversion in Java

Given a number in a given base, convert it into another target base.


Input : Number = "123"
        Source Base = 8
        Target Base = 10
Output : 83
3 * 1 + 2 * 8 + 1 * 64 = 83

Input : Number = "110"
        Source Base = 2
        Target Base = 10 
Output : 6

The idea is to use toString() method present in Integer wrapper class. We also use partseInt() to parse given string representation of number in a given base.





// Java program to convert one base to other 
public class MainClass {
    public static String baseConversion(String number,
                                 int sBase, int dBase)
        // Parse the number with source radix 
        // and return in specified radix(base)
        return Integer.toString(
            Integer.parseInt(number, sBase),
    public static void main(String[] args)
        String number = "555"; // Number
        int sBase = 8; // Source Base Octal
        int dBase = 10; // Destination Base Decimal
        System.out.println("Octal to Decimal: "
            + baseConversion(number, sBase, dBase));
        dBase = 16; // Destination Base Hexadecimal
        System.out.println("Octal to Hex: "
            + baseConversion(number, sBase, dBase));



Octal to Decimal: 365
Octal to Hex: 16d

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.