Check this out: https://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html#toString(byte)
toString
public static String toString(byte b)
Returns a new String object representing the specified byte. The radix
is assumed to be 10.
Parameters: b - the byte to be converted Returns: the string
representation of the specified byte
See Also: Integer.toString(int)
The issue is that you're turning the string into bytes, but then you're going
and turning that byte into a string/character (interpreted as a base 10 number - radix=10) which means you essentially get the ascii equivalent of each character (c=99, o=111, o=111, l=108) which is a number in base 10. However you're the numeric character for each digit. When you go to turn the string back into a byte you're getting the byte for the numeric character not the byte for the letter like you want.
Depending on what you're actually after, you're going to need to find a different approach. It's not clear what you are trying to show by converting to bytes, but if you really want to convert to and from a bitstring (a string composed of the numeric characters for 0s and 1s) you'll have to do more work.
If you delimited the string you're building with some other character like a comma (e.g. 99,111,111,108) then you could assume the delimited substrings were integers (for regular ascii) and pass them to 'Integer.parseInt(s)' or 'Integer.valueOf(s)' and then do a conversion to char and then build the chars into a string.
For example:
StringBuilder sb = new StringBuilder();
String str = "99,111,111,108"; // result of initial conversion
String[] sa = str.split(",");
char ch = '';
for(String s : sa) {
ch = Integer.parseInt(s);
sb.append(ch);
}
FileOutputStream fos = new FileOutputStream("new.txt");
fos.write(sb.toString().getBytes());
fos.close();
An important note here is that, for Java at least, chars are just integers except that they a char is interpreted as being an ascii character.
The basic dilemma is, I believe, that converting the bytes to strings is a destructive operation where the context is lost. I.e. the computer no longer knows anything about the original bytes, only what the newly generated string is. Bytes are binary data, but strings are a group of characters (generally ascii, but also UTF of various kinds).