public static String byteArrayToJsonString(byte[] bytes){
  StringBuilder sb=new StringBuilder(bytes.length * 2);
  for (  byte b : bytes) {
    if (b >= 32 && b < 127) {
      char c=(char)b;
      if (c != '%' && c != '\\' && c != '\"') {
        sb.append(c);
        continue;
      }
    }
    sb.append(String.format("%%%02x",b));
  }
  return sb.toString();
}

public static String byteArrayToJsonString ( byte [ ] bytes ) { StringBuilder sb = new StringBuilder ( bytes . length * 2 ) ; for ( byte b : bytes ) { if ( b >= 32 && b < 127 ) { char c = ( char ) b ; if ( c != '%' && c != '\\' && c != '\"' ) { sb . append ( c ) ; continue ; } } sb . append ( String . format ( "%%%02x" , b ) ) ; } return sb . toString ( ) ; }
converts the given array of bytes into a legal json string . < p > uses a simple strategy of converting each byte to a single char , except for non - printable chars , non - ascii chars , and ' % ' , ' \ ' , and ' " ' , which are encoded as three chars in ' % xx ' format , where ' xx ' is the hexadecimal encoding of the byte .
