top

Search

Software Key Tutorial

.

UpGrad

Software Key Tutorial

Octal to Decimal

Introduction

Octal and decimal are numerical systems used to represent numbers in different bases. Octal is a base-8 system that uses digits from 0 to 7, while decimal is a base-10 system that uses digits from 0 to 9. Converting numbers from octal to decimal is common in programming and mathematics. 

Understanding the process of octal to decimal conversion is essential for working with octal numbers and translating them into a format widely used in everyday calculations.

Overview

The process of converting octal to decimal involves transforming a number represented in the octal system into its equivalent representation in the decimal system. This conversion relies on the positional value of each digit in the octal number. Starting from the rightmost digit, each digit is multiplied by the corresponding power of 8 and summed to obtain the decimal equivalent. 

Following a straightforward algorithm can perform the conversion efficiently and accurately.

Algorithm to Convert Octal to Decimal

To convert octal numbers to decimal, we follow a simple algorithm. We start from the rightmost digit of the octal number and multiply each digit by the appropriate power of 8. We then sum up the products to obtain the decimal equivalent. 

Let's illustrate this algorithm with a few examples:

Octal to Decimal Conversion Examples: 

Example 1: 32

  1. Step 1: Start from the rightmost digit, which is 2.

  2. Step 2: Multiply 2 by 8^0 (1) to get 2.

  3. Step 3: Move to the next digit, which is 3.

  4. Step 4: Multiply 3 by 8^1 (8) to get 24.

  5. Step 5: Add the products: 2 + 24 = 26.

Therefore, the octal number 32 is equivalent to the decimal number 26.

Here's a code snippet in Python that converts 32 octal to decimal:

octal = '32'
decimal = 0

for i in range(len(octal)):
    digit = int(octal[len(octal) - i - 1])
    decimal += digit * (8 ** i)

print("Decimal equivalent:", decimal)

Output:


In this code, we iterate over each digit of the octal number from right to left. We convert each digit to an integer and multiply it by the corresponding power of 8. The result is accumulated in the decimal variable. Finally, we print the decimal equivalent of the octal number.

Example 2: Octal number 17

Step 1: Start from the rightmost digit, which is 7.

Step 2: Multiply 7 by 8^0 (1) to get 7.

Step 3: Move to the next digit, which is 1.

Step 4: Multiply 1 by 8^1 (8) to get 8.

Step 5: Add the products: 7 + 8 = 15.

Here's the code in Java for each of the above algorithms:

public class OctalToDecimalExample1 {
    public static void main(String[] args) {
        int octal = 17;
        int decimal = 0;
        int power = 0;
        
        while (octal != 0) {
            decimal += (octal % 10) * Math.pow(8, power);
            octal /= 10;
            power++;
        }
        
        System.out.println("Octal: 17");
        System.out.println("Decimal: " + decimal);
    }
}

Output:

Example 3: Octal number 64

Step 1: Start from the rightmost digit, which is 4.

Step 2: Multiply 4 by 8^0 (1) to get 4.

Step 3: Move to the next digit, which is 6.

Step 4: Multiply 6 by 8^1 (8) to get 48.

Step 5: Add the products: 4 + 48 = 52.

Here's the code in Java for each of the above algorithms:

 public class OctalToDecimalExample2 {
    public static void main(String[] args) {
        int octal = 64;
        int decimal = 0;
        int power = 0;
        
        while (octal != 0) {
            decimal += (octal % 10) * Math.pow(8, power);
            octal /= 10;
            power++;
        }
        
        System.out.println("Octal: 64");
        System.out.println("Decimal: " + decimal);
    }
}

Output:


In each case, the code uses a while loop to iterate through the octal digits, performs the necessary calculations based on the algorithm, and finally prints the original octal number and its corresponding decimal equivalent.

Algorithm to Convert Octal to Hexadecimal

In addition to converting octal numbers to decimal, we can also convert them to hexadecimal. The process is similar, but instead of multiplying by powers of 8, we multiply by powers of 16. This allows us to represent octal numbers in hexadecimal notation.

To convert an octal number to hexadecimal, follow these steps:

  1. Start with the given octal number.

Group the octal digits from right to left into sets of three digits each. If the number of digits is not divisible by three, add leading zeros to form complete groups.

  1. Replace each group of three octal digits with the corresponding hexadecimal digit.

Group: 0 1 2 3 4 5 6 7

Hex: 0 1 2 3 4 5 6 7

  1. Concatenate the hexadecimal digits obtained from each group to form the hexadecimal equivalent of the octal number.

Example 1: Convert octal number 317 to hexadecimal:

  • Step 1: Group the octal digits: 0317

  • Step 2: Replace each group with the corresponding hexadecimal digit:

Group: 0 3 1 7

Hex: 0 3 1 7

  • Step 3: Concatenate the hexadecimal digits: 0317 (octal) = 0317 (hexadecimal)

Therefore, the octal number 0317 is equivalent to the hexadecimal number 0317.

Note: If there are leading zeros in the resulting hexadecimal number, they can be omitted.

Here's the Java code that converts an octal number to its hexadecimal equivalent:

public class OctalToHexadecimal {
    public static void main(String[] args) {
        String octal = "317";
        String hexadecimal = "";

        // Group the octal digits into sets of three from right to left
        int index = octal.length() - 1;
        while (index >= 0) {
            // Extract the current group of three octal digits
            String group = octal.substring(Math.max(index - 2, 0), index + 1);
            
            // Convert the group to decimal
            int decimal = Integer.parseInt(group, 8);
            
            // Convert the decimal to hexadecimal
            String hex = Integer.toHexString(decimal);
            
            // Append the hexadecimal digit(s) to the result
            hexadecimal = hex + hexadecimal;
            
            // Move to the next group
            index -= 3;
        }

        System.out.println("Hexadecimal equivalent: " + hexadecimal);
    }
}

Output:


The Java code converts an octal number to its hexadecimal equivalent. It groups the octal digits into sets of three from right to left. Each group is converted from octal to decimal and then to hexadecimal. 

The resulting hexadecimal digits are concatenated to form the final hexadecimal representation. In this case, the octal number 317 is converted to its hexadecimal equivalent, which is 713.

Example 1: Octal number 17 to hexadecimal

octal = '17'
binary = ''
hexadecimal = ''

for digit in octal:
    binary += format(int(digit), '03b')

for i in range(0, len(binary), 4):
    group = binary[i:i+4]
    hexadecimal += format(int(group, 2), 'X')

print("Octal: " + octal)
print("Hexadecimal: " + hexadecimal)

Output:


Example 2: Octal number 46 to hexadecimal

octal = '46'
binary = ''
hexadecimal = ''

for digit in octal:
    binary += format(int(digit), '03b')

for i in range(0, len(binary), 4):
    group = binary[i:i+4]
    hexadecimal += format(int(group, 2), 'X')

print("Octal: " + octal)
print("Hexadecimal: " + hexadecimal)

Output:


In each case, the code converts the octal number to its binary representation and then groups the binary digits into sets of four. The binary groups are then converted to their corresponding hexadecimal representation using the format() function. 

Program to Convert Octal to Decimal in C

Converting octal to decimal in C is straightforward. We can utilize the built-in functions and data types to perform the conversion efficiently. Here's an example program that demonstrates the conversion:

// Code snippet for converting octal to decimal in C
#include <stdio.h>

int main() {
   int octal, decimal = 0, power = 0;
   
   printf("Enter an octal number: ");
   scanf("%d", &octal);
   
   while (octal != 0) {
      decimal += (octal % 10) * pow(8, power);
      octal /= 10;
      power++;
   }
   
   printf("Decimal equivalent: %d", decimal);
   
   return 0;
}

Let's assume the octal number entered is 236. The output will be:


The given code snippet in C allows for the conversion of octal numbers to their decimal equivalents. It begins by accepting user input for an octal number, which is then stored in the variable octal. 

Through a while loop, the code iteratively extracts the rightmost digit of the octal number, multiplies it by the appropriate power of 8, and adds the result to the decimal variable. 

Once the loop completes, the calculated decimal equivalent is displayed using printf. This code provides a convenient way to convert octal numbers to decimal representations in C programming.

Program to Convert Octal to Decimal in Java

Java provides powerful features to facilitate octal to decimal conversion. We can leverage the Integer class and its methods to achieve the desired result. Take a look at the following Java program:

// Code snippet for converting octal to decimal in Java
import java.util.Scanner;

public class OctalToDecimal {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an octal number: ");
        String octalStr = scanner.nextLine();
        int decimal = Integer.parseInt(octalStr, 8);
        System.out.println("Decimal equivalent: " + decimal);
    }
}

Assuming you entered the octal number "27". Here's the output for the given code snippet:


The above Java code converts an octal number to its decimal equivalent. It prompts the user to enter an octal number, parses it using Integer.parseInt() with a radix of 8 (indicating octal), and then prints the calculated decimal equivalent.

Program to Convert Octal to Decimal Python

Python provides a simple and intuitive way to convert octal to decimal using built-in functions. Here's an example Python program:

# Code snippet for converting octal to decimal in Python
octal = input("Enter an octal number: ")
decimal = int(octal, 8)
print("Decimal equivalent:", decimal)

Output:

The Python code snippet prompts users to enter an octal number using input(). The inputted value is stored in the variable octal.

Then, the int() function converts the octal variable to its decimal equivalent. The second argument 8 specifies that the input is in base 8 (octal).

Finally, the code prints the calculated decimal equivalent using print(). The output shows the entered octal number and its corresponding decimal equivalent.

Conclusion

Converting octal numbers to decimals is a fundamental skill in programming and mathematics. It allows us to work with octal numbers in a more widely understood and used format. We can seamlessly translate octal representations into decimal equivalents by applying the conversion algorithm. 

Whether solving mathematical problems or working with octal-based systems, understanding octal to decimal conversion expands our capabilities and enhances our comprehension of numerical systems.

FAQs

  1. How do I convert an octal number to decimal in Java?

To convert an octal number to decimal in Java, you can use a loop to extract the digits of the octal number and multiply them by the corresponding powers of 8. Keep accumulating the results to obtain the decimal equivalent.

  1. How can I convert octal to hexadecimal in Python?

To convert octal to hexadecimal in Python, you can first convert the octal number to binary. Then, group the binary digits into sets of four and convert each group to its hexadecimal equivalent using the format() function or other suitable methods.

  1. How does the algorithm for octal to decimal conversion work?

The algorithm for octal to decimal conversion involves multiplying each digit of the octal number by the appropriate power of 8 and summing the results. Starting from the rightmost digit, each digit is multiplied by 8 and raised to the power corresponding to its position. The products are then added to obtain the decimal equivalent.

  1. How do I convert an octal number to hexadecimal in C?

In C, you can convert an octal number to hexadecimal by first converting the octal number to decimal using the algorithm mentioned earlier. Once you have the decimal equivalent, you can use appropriate functions or techniques to convert the decimal number to hexadecimal representation, such as using the %X format specifier with printf().

Leave a Reply

Your email address will not be published. Required fields are marked *