Sai A Sai A
Updated date Jul 29, 2023
In this blog, we explore Java string manipulation and various methods to convert a regular string into a binary string array. We begin with a simple approach using Integer.toBinaryString(), and then proceed to a more efficient method using bitwise operations and a StringBuilder.

Introduction:

In this blog, we will explore different methods to convert a regular Java string into a binary string array. By the end of this blog, you will have a complete understanding of various techniques for converting a Java string to a binary string array.

Method 1: Using Integer.toBinaryString()

The first method involves using Java's built-in Integer.toBinaryString() method, which converts an integer to its binary string representation. By iterating over each character in the input string, we convert them to their binary representation and store them in the output array.

public class StringToBinaryConverter {
    public static String[] convertToBinaryArray(String input) {
        String[] binaryArray = new String[input.length()];
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            binaryArray[i] = Integer.toBinaryString(ch);
        }
        return binaryArray;
    }

    public static void main(String[] args) {
        String inputString = "Hello";
        String[] binaryArray = convertToBinaryArray(inputString);
        System.out.println("Method 1 Output:");
        for (String binaryString : binaryArray) {
            System.out.print(binaryString + " ");
        }
    }
}

Output:

1001000 1100101 1101100 1101100 1101111

Method 2: Using StringBuilder and Bitwise Operations

In this method, we take advantage of bitwise operations and a StringBuilder to convert each character to its binary representation. By using bitwise right shift and bitwise AND operations, we extract individual bits and append them to the StringBuilder in reverse order to form the binary string.

public class StringToBinaryConverter {
    public static String[] convertToBinaryArray(String input) {
        String[] binaryArray = new String[input.length()];
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            StringBuilder binaryStringBuilder = new StringBuilder();
            for (int j = 7; j >= 0; j--) {
                binaryStringBuilder.append((ch >> j) & 1);
            }
            binaryArray[i] = binaryStringBuilder.toString();
        }
        return binaryArray;
    }

    public static void main(String[] args) {
        String inputString = "Hello";
        String[] binaryArray = convertToBinaryArray(inputString);
        System.out.println("\nMethod 2 Output:");
        for (String binaryString : binaryArray) {
            System.out.print(binaryString + " ");
        }
    }
}

Output:

01001000 01100101 01101100 01101100 01101111

Conclusion:

In this blog, we explored two different methods to convert a Java string to a binary string array. The first method utilized Integer.toBinaryString() to convert characters to binary strings directly. The second method, on the other hand, employed bitwise operations with a StringBuilder for efficient conversion.

Comments (0)

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