<p>I have binary data in a file that I can read into a byte array and process with no problem.  Now I need to send parts of the data over a network connection as elements in an XML document.  My problem is that when I convert the data from an array of bytes to a String and back to an array of bytes, the data is getting corrupted.  I've tested this on one machine to isolate the problem to the String conversion, so I now know that it isn't getting corrupted by the XML parser or the network transport.</p>

<p>What I've got right now is</p>

<pre><code>byte[] buffer = ...; // read from file
// a few lines that prove I can process the data successfully
String element = new String(buffer);
byte[] newBuffer = element.getBytes();
// a few lines that try to process newBuffer and fail because it is not the same data anymore
</code></pre>

<p>Does anyone know how to convert binary to String and back without data loss?</p>

<p>Answered:  Thanks Sam.  I feel like an idiot.  I had this answered yesterday because my SAX parser was complaining.  For some reason when I ran into this seemingly separate issue, it didn't occur to me that it was a new symptom of the same problem.</p>

<p>EDIT:  Just for the sake of completeness, I used the <a href="http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html" rel="noreferrer">Base64</a> class from the <a href="http://commons.apache.org/" rel="noreferrer">Apache Commons</a> <a href="http://commons.apache.org/codec/" rel="noreferrer">Codec</a> package to solve this problem.</p>

[{'answer_id': 20779, 'author': 'Sam', 'author_id': 428, 'author_profile': 'https://Stackoverflow.com/users/428', 'pm_score': 5, 'selected': True, 'text': '<p>If you encode it in base64, this will turn any data into ascii safe text, but base64 encoded data is larger than the orignal data</p>\n'}, {'answer_id': 20831, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 0, 'selected': False, 'text': "<p>How are you building your XML document?  If you use java's built in XML classes then the string encoding should be handled for you.</p>\n\n<p>Take a look at the javax.xml and org.xml packages.  That's what we use for generating XML docs, and it handles all the string encoding and decoding quite nicely.</p>\n\n<p>---EDIT:</p>\n\n<p>Hmm, I think I misunderstood the problem.  You're not trying to encode a regular string, but some set of arbitrary binary data?  In that case the Base64 encoding suggested in an earlier comment is probably the way to go.  I believe that's a fairly standard way of encoding binary data in XML.</p>\n"}, {'answer_id': 20844, 'author': 'basszero', 'author_id': 287, 'author_profile': 'https://Stackoverflow.com/users/287', 'pm_score': 2, 'selected': False, 'text': '<p>See this question, <a href="https://stackoverflow.com/questions/19893">How do you embed binary data in XML?</a>\nInstead of converting the byte[] into String then pushing into XML somewhere, convert the byte[] to a String via BASE64 encoding (some XML libraries have a type to do this for you). The BASE64 decode once you get the String back from XML.</p>\n\n<p>Use <a href="http://commons.apache.org/codec/" rel="nofollow noreferrer">http://commons.apache.org/codec/</a></p>\n\n<p>You data may be getting messed up due to all sorts of weird character set restrictions and the presence of non-priting characters. Stick w/ BASE64.</p>\n'}, {'answer_id': 20860, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 5, 'selected': False, 'text': '<p><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html" rel="noreferrer">String(byte[])</a> treats the data as the default character encoding. So, how bytes get converted from 8-bit values to 16-bit Java Unicode chars will vary not only between operating systems, but can even vary between different users using different codepages on the same machine! This constructor is only good for decoding one of your own text files. Do not try to convert arbitrary bytes to chars in Java!</p>\n\n<p>Encoding as <a href="http://en.wikipedia.org/wiki/Base64" rel="noreferrer">base64</a> is a good solution. This is how files are sent over SMTP (e-mail). The (free) Apache <a href="http://commons.apache.org/codec/" rel="noreferrer">Commons Codec</a> project will do the job.</p>\n\n<pre class="lang-java prettyprint-override"><code>byte[] bytes = loadFile(file);          \n//all chars in encoded are guaranteed to be 7-bit ASCII\nbyte[] encoded = Base64.encodeBase64(bytes);\nString printMe = new String(encoded, "US-ASCII");\nSystem.out.println(printMe);\nbyte[] decoded = Base64.decodeBase64(encoded);\n</code></pre>\n\n<p>Alternatively, you can use the Java 6 <a href="http://download.oracle.com/javase/6/docs/api/javax/xml/bind/DatatypeConverter.html" rel="noreferrer">DatatypeConverter</a>:</p>\n\n<pre class="lang-java prettyprint-override"><code>import java.io.*;\nimport java.nio.channels.*;\nimport javax.xml.bind.DatatypeConverter;\n\npublic class EncodeDecode {    \n  public static void main(String[] args) throws Exception {\n    File file = new File("/bin/ls");\n    byte[] bytes = loadFile(file, new ByteArrayOutputStream()).toByteArray();\n    String encoded = DatatypeConverter.printBase64Binary(bytes);\n    System.out.println(encoded);\n    byte[] decoded = DatatypeConverter.parseBase64Binary(encoded);\n    // check\n    for (int i = 0; i &lt; bytes.length; i++) {\n      assert bytes[i] == decoded[i];\n    }\n  }\n\n  private static &lt;T extends OutputStream&gt; T loadFile(File file, T out)\n                                                       throws IOException {\n    FileChannel in = new FileInputStream(file).getChannel();\n    try {\n      assert in.size() == in.transferTo(0, in.size(), Channels.newChannel(out));\n      return out;\n    } finally {\n      in.close();\n    }\n  }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/20778', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1288/']