Convert From Any Base to Decimal and Vice Versa in Java5 May 2025 | 5 min read Calculate the decimal representation of a given number and its base. Any number that can be represented by the numbers 0 through 9 and A through Z can be used as the base of the number. The value of A is 10, and the value of B is 11, and so on. The number can be converted to decimal by implementing a function. Example 1: Input: Output: The equivalent value is 485 Explanation: The formula that follows is used to convert the octal number 745 to decimal: (7 * 8^2) + (4 * 8^1) + (5 * 8^0) = 448 + 32 + 5 = 485 Example 2: Input: Output: The equivalent value is 11 Explanation: The following formula is used to convert the binary number 1011 to decimal: (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 8 + 0 + 2 + 1 = 11. Example 3: Input: Output: The equivalent value is 675 Explanation: The following formula is used to convert the Hexadecimal 2A3 to decimal: (2 × 16^2) + (A × 16^1) + (3 × 16^0) = 512 + 160 + 3 = 675, where 'A' represents the 0 in hexadecimal. Thus, 2A3 in hexadecimal is 675 in decimal. Example 4: Input: Output: The equivalent value is 10010. Explanation: To convert 18 to binary, keep track of the remainder after continuously dividing by 2: 18 ÷ 2 = 9 remainder 0 9 ÷ 2 = 4 remainder 1 4 ÷ 2 = 2 remainder 0 2 ÷ 2 = 1 remainder 0 1 ÷ 2 = 0 remainder 1 The binary is 10010 when read from the bottom to the top. Example 5: Input: Output: The equivalent value is FF. Explanation: The hexadecimal representation of 255 is FF, which may be obtained by repeatedly dividing by 16 and taking the remainders: 255 ÷ 16 = 15 (F) remainder 15 (F). Hence, the value is FF. Approach: NaïveA character can be converted to its equivalent integer value using the value function defined in the code, and base-to-decimal conversion can be done using the toDeci function. By changing their ASCII values, the value function works with both alphabetic characters and numeric digits. The toDeci function multiplies the value of each character by the matching power of the base as it iterates across the string from right to left. Invalid digits-those that exceed the base-are checked for, and if found, an error is returned. Implementation:FileName: baseTodecimal.java Output: The Decimal equivalent of the 2A3 with the base 16 is given as 675 Complexity Analysis: The time complexity of the above code is O(N), where 'N' represents the length of the string, and the space complexity is O(1). How to do the reverse?Approach: NaïveThe code implements a decimal-to-any-base conversion scheme. With support for both numeric and alphanumeric digits up to base 36, the revValue method determines the character representation for an integer value. In order to create the proper base representation, the fromDecimal method calculates remainders, transforms them into characters, and then reverses the resultant sequence. Implementation:FileName: DecimalConversion.java Output: The Equivalent of the 282 with the base 16 is 11A Complexity Analysis: The Time Complexity is O(logbasen) where 'n' is the input number the 'base' represents the to which the number is to be converted and the space complexity is O(logbasen). Approach: Using Integer.parseInt()A string representation of a number in the given base (radix) is converted to its decimal integer equivalent using the Integer.parseInt(String s, int radix) method, as the code shows. Implementation:FileName: IntFunctionMethod.java Output: The equivalent value is 12 Next TopicVert.x Java |
Difference between Error and Exception in Java
The general meaning of exception is a deliberate act of omission, while the meaning of error is an action that is inaccurate or incorrect. In Java, Exception and Error are both subclasses of the Java Throwable class that belongs to java.lang package. But there exist...
3 min read
DoubleBuffer put() methods in Java with Examples
The java.nio.DoubleBuffer has a put(double f) method. DoubleBuffer Class is used to increase the position after writing the given double into the dynamically formed double buffer at the current location. Syntax: public abstract DoubleBuffer put(double f) Parameters: The double value f, which needs to be written...
8 min read
How to Modify HTML File Using Java
? In order to modify an HTML file using Java, we can use the Java DOM API. The DOM (Document Object Model) API is a platform-independent, language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of HTML documents. Here...
11 min read
Time Class Operation in Java
In Java especially in applications, managing time as well as date is one of those tasks that is very common. JDK 8 contains time package that includes the set of classes for working with the time and date of these, the LocalTime class is particularly created...
5 min read
Fail-fast and Fail-safe in Java
Fail-fast and Fail-safe are the iterators or collections in Java. Java SE specification doesn't use the Fail-safe term. We use the Fail-safe to segregate between the Non-Fail-fast and Fail-fast iterators. The Fail-Fast system terminates the operation as-fast-as-possible that are exposing failures and stop the entire operation....
6 min read
What is Framework in Java
Java Framework is the body or platform of pre-written codes used by Java developers to develop Java applications or web applications. In other words, Java Framework is a collection of predefined classes and functions that is used to process input, manage hardware devices interacts with system...
4 min read
Java boolean Keyword
In Java, the boolean keyword is a primitive data type. It is used to store only two possible values, either true or false. It specifies 1-bit of information and its "size" can't be defined precisely. The boolean keyword is used with variables and methods. Its default...
1 min read
How to Achieve Performance in Multithreading in Java?
It is essential to maximize the potential of modern multi-core CPUs to enhance the performance of Java applications in the current computing landscape. Multithreading is crucial for achieving the goal because it enables the performance of numerous tasks simultaneously. However, to achieve effective multithreading in Java,...
3 min read
Java Program to Check Given Matrix is Magic Square or Not
A magic square is a square matrix (an (n times n) grid) of unique positive integers organized in such a way that the total of the numbers in each row, column, and both main diagonals is equal. This constant sum is known as the magical constant....
7 min read
FloatBuffer equals() method in Java with examples
The java.nio.FloatBuffer class has equals() function. To determine whether the buffer that is provided is equal to another object, utilize the FloatBuffer Class. If and only if two float buffers are equivalent, then The element type is the same for both. The number of remaining elements is the...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India