Question

Here is what i am trying to do. I have a string :

String s="ch"

I convert it into a binary string in the following way

char ar[]=s.toCharArray();
StringBuilder sb= new StringBuilder("00"); /* i am appending to extra zeros because                        
                                                  when i convert "ch" to binary string it   
                                                 consists of 14 characters(0s and 1s) and i                 
                                               need them to be a multiple of 8, so i add 2 
                                                0s to make it 16)*/
String wm="  ";
for(char c:ar)
{
    wm=Integer.toBinaryString((int)c);
    sb.append(wm);
}

Now i want to convert this binary string back into character...such that i get back "ch" as the output. Can anyone help?

Was it helpful?

Solution

You can parse a binary string to an int with Integer#parseInt(String s, int radix), and then cast it to a char:

char result = (char) Integer.parseInt(s, 2);

EDIT Your string will look like this:

0011000111101000
00<--c--><--h-->

To get c and h separately you need to split the String. Try using String#substring() to get the parts of the string belonging to the different letters. All lowercase letters are 7 digits long, so it shouldn't be too difficult.

OTHER TIPS

You can use the Integer.parseInt method to convert it to an int, but you have to mention the radix (2). Afterwards You can convert this int to a String. Like this:

int chars = Integer.parseInt(wm, 2);
String str = new Character((char)chars).toString();

The way how you appended 0 is incorrect. The reason that it's short by two 0s is when c and h are converted to binary, c became 1100011. However, it is supposed to be 01100011 because we are looking at a byte so you need to left pad each binary string and not the final string with 0.

Your final string should be 0110001101101000 and not 0011000111101000.

once you have the binary string properly constructed, then you can use the following code to turn it back to string.

StringBuilder result = new StringBuilder();
for(int i = 0; i < binaryStr.length(); i += 8){
  result.append((char)Integer.parseInt(binaryStr.substring(i, i + 8), 2));
}

There are probably other clever ways of doing it, but this is what I can think of.

One thing to consider when you're converting your characters to binary to make sure you pad left with zeroes so the binary is 8 bits in length. This can be accomplished with something like this:

// "%08d" will pad zeroes to the left if it's not 8 in length
String.format("%08d", Integer.parseInt(Integer.toBinaryString((int)c)))

You're using a StringBuilder to build the binary string, so you would use the above String.format() like so:

String s = "ch";
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
    sb.append(String.format("%08d", Integer.parseInt(Integer.toBinaryString((int)c))));
}
System.out.println(sb);

Results:

|--c---||--h---|
0110001101101000

And the reverse (binary to character) of it would be:

for (int i = 0; i < sb.length(); i += 8) {
    System.out.print((char)Integer.parseInt(sb.substring(i, i + 8), 2));
}
System.out.println();

Results:

ch

All together would look like:

public static void main(String[] args) throws Exception {
    String s = "ch";
    StringBuilder sb = new StringBuilder();
    for (char c : s.toCharArray()) {
        sb.append(String.format("%08d", Integer.parseInt(Integer.toBinaryString((int)c))));
    }
    System.out.println(sb);

    for (int i = 0; i < sb.length(); i += 8) {
        System.out.print((char)Integer.parseInt(sb.substring(i, i + 8), 2));
    }
    System.out.println();
}

Results:

0110001101101000
ch
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top