Base conversion in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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)); } } OutputDecimal : 4271 Octal : 10257 Octal : 1000010101111 Comment More infoAdvertise with us Next Article Interesting Facts About Java B bilal-hungund Follow Improve Article Tags : Java java-wrapper-class Practice Tags : Java Similar Reads Interesting Facts About Java Java: A general-purpose, high-level programming language. It is developed by Sun Microsystems. It was developed by a mini team of engineers which is known as the Green Team. They initiated this language in 1991. Here are some interesting facts about Java: The initial name of java was "Oak". It was c 2 min read Interesting Facts About Java Java: A general-purpose, high-level programming language. It is developed by Sun Microsystems. It was developed by a mini team of engineers which is known as the Green Team. They initiated this language in 1991. Here are some interesting facts about Java: The initial name of java was "Oak". It was c 2 min read Coding Guidelines in Java Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons 7 min read Java 11 - Features and Comparison Every 6 months, Oracle releases new Java versions. The September Java 11 version is already making a lot of buzz in the computer science world even before it was released by declaring that from now on Java is gonna be paid for commercial use. The following articles will show important features, enha 5 min read Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class is a template to create objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, 10 min read Basic Operators in Java Java provides a rich operator environment. We can classify the basic operators in java in the following groups: Arithmetic OperatorsRelational OperatorsBitwise OperatorsAssignment OperatorsLogical Operators Let us now learn about each of these operators in detail. 1. Arithmetic Operators: Arithmetic 10 min read Like