1

I am new in Java.I have changed binary String to text string Using this code.

   StringBuilder contentBuilder = new StringBuilder();
 StringBuffer sb = new StringBuffer();
try {
    BufferedReader in = new BufferedReader(new FileReader("filename.txt"));
    String jitu;
    while ((jitu = in.readLine()) != null) {
        contentBuilder.append(jitu);
    }
    in.close();
} catch (IOException e) {
}
    String binput = contentBuilder.toString();     
     for (int i = 0; i < binput.length()/8; i++)
    {
        int a = Integer.parseInt(binput.substring(8*i,(i+1)*8),2);
       sb.append((char)(a));
    }

Then I want to reverse this text to binary by following code.

      byte[] bytes = binput.getBytes();
  StringBuilder binary = new StringBuilder();
  for (byte b : bytes){
     int val = b;
     for (int i = 0; i < 8; i++){
     binary.append((val & 128) == 0 ? 0 : 1);
     val <<= 1;
     }}

But the problem is When I will getting the output from binary.append(). that is not the same as the the input binary string.I want output same as input. Thanks in advance.

jitec291
  • 11
  • 1
  • 1
    Before you try to do this you should take a look at http://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and-vice-versa – René Link Jul 07 '13 at 17:14
  • I suggest you use StringBuilder instead of StringBuffer. The latter became a legacy class more than ten years ago. – Peter Lawrey Jul 07 '13 at 20:33

1 Answers1

0

If your file really contains binary (as in, not character data), then the way you are reading it in is almost guaranteed to lose data. You are transforming binary to a string using the default charset. On many systems, the default charset is UTF-8. Many binary sequences do not map to a valid unicode character, in those cases if you are interpreting that data as UTF-8, the default behavior is to replace that data with a set replacement character. That means you're going to lose a lot of data.

If you really want to read in binary data as a String (which may well not be a good approach), use a charset which has a mapping for every possible bit sequence (such as Windows-1252) so that you can be sure you won't lose any data.

Aurand
  • 5,487
  • 1
  • 25
  • 35