Sai A Sai A
Updated date Jul 14, 2023
In this blog, we will learn different methods to convert a Java string to a binary array. The blog will guide you through two approaches: using the toBinaryString() method and the getBytes() method.

Introduction:

Converting a string to a binary array is a common task in Java, allowing us to manipulate individual bits and perform bitwise operations. In this blog, we will delve into various methods of converting a Java string to a binary array, providing step-by-step explanations and sample code for each approach. By the end, you will have a clear understanding of multiple techniques and their trade-offs.

Method 1: Using the toBinaryString() method

The easiest way to convert a string to a binary array in Java is by leveraging the toBinaryString() method provided by the Integer class. This method converts an integer value to its binary string representation. We can employ this method to convert each character of the string individually.

public class StringToBinaryArrayConverter {
    public static int[] convertStringToBinaryArray(String input) {
        int[] binaryArray = new int[input.length() * 8]; // Each character represents 8 bits

        for (int i = 0; i < input.length(); i++) {
            int character = input.charAt(i);
            String binaryString = Integer.toBinaryString(character);

            for (int j = 0; j < binaryString.length(); j++) {
                binaryArray[i * 8 + j] = binaryString.charAt(j) - '0';
            }
        }

        return binaryArray;
    }

    public static void main(String[] args) {
        String input = "Hello, World!";
        int[] binaryArray = convertStringToBinaryArray(input);

        System.out.println("Binary Array: ");
        for (int bit : binaryArray) {
            System.out.print(bit);
        }
    }
}

Output:

Binary Array:
0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100

Method 2: Using the String.getBytes() method

An alternative approach to convert a string to a binary array is by utilizing the getBytes() method provided by the String class. This method returns a byte array that represents the string using the platform's default charset. We can then extract the bits from each byte and store them in the binary array.

public class StringToBinaryArrayConverter {
    public static int[] convertStringToBinaryArray(String input) {
        byte[] bytes = input.getBytes();
        int[] binaryArray = new int[bytes.length * 8]; // Each byte represents 8 bits

        for (int i = 0; i < bytes.length; i++) {
            for (int j = 0; j < 8; j++) {
                binaryArray[i * 8 + j] = (bytes[i] >> (7 - j)) & 1;
            }
        }

        return binaryArray;
    }

    public static void main(String[] args) {
        String input = "Hello, World!";
        int[] binaryArray = convertStringToBinaryArray(input);

        System.out.println("Binary Array: ");
        for (int bit : binaryArray) {
            System.out.print(bit);
        }
    }
}

Output:

Binary Array:
0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100

Conclusion:

In this blog, we explored two methods to convert a Java string to a binary array. The first method used the toBinaryString() method to convert each character to its binary representation, while the second method employed the getBytes() method to convert the string to a byte array and then extracted the bits. 

Comments (0)

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