Thursday, September 4, 2025
HomeLanguagesJavaBase conversion in Java

Base conversion in Java

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

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 the Integer wrapper class. We also use partseInt() to parse given string representation of number in a given base.
 

Java




// 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), dBase);
    }
    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));
    }
}


Output: 

Octal to Decimal: 365
Octal to Hex: 16d

 

Without using a Predefined method

Java




// Java program to convert one base to other
 
import java.io.*;
import java.util.*;
class Number {
    private int base; // 2, 8, 10, 16
    private String value; // can be of any base
    private Map<Character, Integer>
        hexatoDec; // for hexadecimal to decimal conversion
    private Map<Integer, Character>
        dectoHex; // for decimal to hexadecimal
    public Number(String value, int base)
    {
        this.value = value;
        this.base = base;
        hexatoDec = new HashMap<>();
        dectoHex = new HashMap<>();
        for (int i = 0; i < 6; i++) {
            dectoHex.put(10 + i, (char)('A' + i));
            hexatoDec.put((char)('A' + i), 10 + i);
        }
    }
    public Number(String value)
    {
        // default base 10
        this.base = 10;
        this.value = value;
    }
    public String toDecimal()
    {
        int sum = 0;
        int pow = 0;
        String tempVal = this.value;
        for (int i = tempVal.length() - 1; i >= 0; i--) {
            int val = tempVal.charAt(i) - '0';
            if (this.base == 16
                && hexatoDec.containsKey(
                    tempVal.charAt(i))) {
                val = hexatoDec.get(tempVal.charAt(i));
            }
            sum += (val) * (Math.pow(this.base, pow++));
        }
        return String.valueOf(sum);
    }
    public String toBase(int targetBase)
    {
        // take the given number
        // convert it into decimal
        // divide the decimal with the target base
        String val = this.value;
 
        // must be in decimal
        if (this.base != 10)
            val = toDecimal();
        int temp = Integer.parseInt(val);
        StringBuilder str = new StringBuilder();
        while (temp != 0) {
            int tempValue = temp % targetBase;
            if (targetBase == 16
                && dectoHex.containsKey(tempValue)) {
                str.insert(0, dectoHex.get(tempValue));
            }
            else {
                str.insert(0, tempValue);
            }
            temp /= targetBase;
        }
        return str.toString();
    }
}
class GFG {
    public static void main(String[] args)
    {
        Number n1 = new Number("10AF", 16);
        System.out.println("Decimal : " + n1.toDecimal());
        System.out.println("Octal : " + n1.toBase(8));
        System.out.println("Binary : " + n1.toBase(2));
    }
}


Output

Decimal : 4271
Octal : 10257
Octal : 1000010101111
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS