Java Program to Convert Octal Number to Decimal and vice-versa

Updated on December 2, 2024
Convert octal number to decimal and vice-versa header image

Introduction

Java offers straightforward methods to handle conversions between different numerical bases, such as octal and decimal. This capability is particularly useful in applications related to computing, electronics, and data processing where base conversions are frequently required for operations like assembly language programming, debugging, and more.

In this article, you will learn how to create a Java program that can convert an octal number to a decimal and vice-versa. Explore detailed examples that demonstrate the process of conversion using Java's integer parsing capabilities and mathematical calculations, enhancing your ability to handle various data formats efficiently.

Octal to Decimal Conversion

Understanding the Conversion Process

  1. Recognize that octal numbers are base-8.
  2. Realize that each digit in an octal number represents a power of 8, starting from 0 on the far right.

Java Implementation

  1. Use the Integer.parseInt() method with radix 8 to convert an octal string to a decimal integer.
  2. Display the result.
java
public class OctalToDecimal {
    public static int convertOctalToDecimal(String octal) {
        return Integer.parseInt(octal, 8);
    }

    public static void main(String[] args) {
        String octalNumber = "17";  // Example octal number
        int decimal = convertOctalToDecimal(octalNumber);
        System.out.println("Decimal Equivalent : " + decimal);
    }
}

This code snippet defines a method convertOctalToDecimal which uses Integer.parseInt() where '8' signifies that the incoming number is in base 8. The main method tests this conversion with an octal number "17", which outputs its decimal equivalent.

Decimal to Octal Conversion

Understanding the Conversion Process

  1. Acknowledge that converting from decimal to octal involves dividing the number by 8.
  2. Build the octal number from the remainders of these divisions.

Java Implementation

  1. Use the Integer.toOctalString() method to convert a decimal number into an octal string.
  2. Display the result.
java
public class DecimalToOctal {
    public static String convertDecimalToOctal(int decimal) {
        return Integer.toOctalString(decimal);
    }

    public static void main(String[] args) {
        int decimalNumber = 15;  // Example decimal number
        String octal = convertDecimalToOctal(decimalNumber);
        System.out.println("Octal Equivalent : " + octal);
    }
}

This code snippet demonstrates the method convertDecimalToOctal, which leverages Integer.toOctalString() to turn a decimal number into its octal form. The main method uses the decimal number '15', converting it to its octal equivalent for display.

Conclusion

Converting between octal and decimal numbers in Java is a straightforward task thanks to Java’s built-in methods like Integer.parseInt() and Integer.toOctalString(). By utilizing these techniques, you efficiently manage base conversions that are crucial in various programming and electronic tasks. The methods and examples provided offer an effective way to implement such conversions, ensuring your code is efficient and clear. Whether for educational purposes or practical application, these conversion methods are invaluable in software development.