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.