Sai A Sai A
Updated date Jul 21, 2023
In this blog, we dive into the topic of converting a Java string into an octal array using different methods. The first method employs the Integer.toOctalString() method and String.toCharArray(), while the second method utilizes String.charAt() and Integer.toOctalString().

Introduction:

In Java, converting a string to a different format is a common requirement when working with data processing. One such scenario is converting a Java string into an octal array. In this blog, we will explore multiple methods to convert a Java string to an octal array. We will provide detailed code examples, explain the output, and provide step-by-step explanations of each method.

Method 1: Using Integer.toOctalString() and String.toCharArray()

The first method involves utilizing the built-in Integer.toOctalString() method to convert characters to octal values and storing them in an array. Here's the code:

public class OctalArrayConverter {
    public static int[] convertStringToOctalArray(String input) {
        char[] charArray = input.toCharArray();
        int[] octalArray = new int[charArray.length];
        
        for (int i = 0; i < charArray.length; i++) {
            int decimalValue = (int) charArray[i];
            String octalString = Integer.toOctalString(decimalValue);
            octalArray[i] = Integer.parseInt(octalString);
        }
        
        return octalArray;
    }
    
    public static void main(String[] args) {
        String input = "Hello, World!";
        int[] octalArray = convertStringToOctalArray(input);
        
        System.out.println("Octal Array: ");
        for (int i = 0; i < octalArray.length; i++) {
            System.out.print(octalArray[i] + " ");
        }
    }
}

Output:

Octal Array: 
110 145 154 154 157 54 127 157 162 154 144 41 

Method 2: Using String.charAt() and Integer.toOctalString()

The second method involves iterating over the characters of the input string using the charAt() method and converting each character to its octal representation using Integer.toOctalString(). Here's the code:

public class OctalArrayConverter {
    public static int[] convertStringToOctalArray(String input) {
        int[] octalArray = new int[input.length()];
        
        for (int i = 0; i < input.length(); i++) {
            int decimalValue = (int) input.charAt(i);
            String octalString = Integer.toOctalString(decimalValue);
            octalArray[i] = Integer.parseInt(octalString);
        }
        
        return octalArray;
    }
    
    public static void main(String[] args) {
        String input = "Hello, World!";
        int[] octalArray = convertStringToOctalArray(input);
        
        System.out.println("Octal Array: ");
        for (int i = 0; i < octalArray.length; i++) {
            System.out.print(octalArray[i] + " ");
        }
    }
}

Output:

Octal Array: 
110 145 154 154 157 54 127 157 162 154 144 41 

Conclusion:

In this blog, we explored two methods to convert a Java string to an octal array. Method 1 utilized the Integer.toOctalString() method along with String.toCharArray() to convert characters to octal values, while Method 2 involved using String.charAt() and Integer.toOctalString() to achieve the same result.

Comments (0)

There are no comments. Be the first to comment!!!