title: Convert byte array to escaped string
body: <p>I need some help converting a java byte array to a 7-Bit ASCII string. However I am getting 8-bit sequences and need to escape any unreadable character to it's escaped sequence. Is there a simple solution for this or do I need to build my own?</p>
  
  <p>Seeing that the range of readable characters in 7-bit ASCII is continuous right now I am thinking of the following:</p>
  
  <pre><code>for( int i = 0; i &lt; buffer.length; i++ ) {
     int codePoint = ( (int) buffer[ i ] ) &amp; 255;
     if( 0x20 &lt;= codePoint &amp;&amp; codePoint &lt;= 0x7e ) {
        res = res + String( (char) codePoint );
     } else {
       String c = Integer.toHexString( codePoint );
       if( c.length() &lt; 2 ) {
         c = "0" + c;
       }
       res = res + "\\0x" + c;
     }
  }
  </code></pre>
  
  <p>However this seems like an awful lot of work for such a simple conversion. Is there a better way?</p>
  
  <p>Also I might need to do the same to data that has been converted from the byte array to strings. Is there a simpler solution in this case?</p>