Пример #1
0
 public static String printable(char ch) {
   if (ch == '\n') {
     return "\\n";
   }
   if (ch == '\r') {
     return "\\r";
   }
   if (ch == '\t') {
     return "\\t";
   }
   if (ch == ' ') {
     return "_";
   }
   if (ch > 127 || ch < 32) {
     StringBuffer sb = new StringBuffer(6);
     sb.append("\\u");
     String hex = Integer.toHexString((int) ch);
     for (int i = 0, len = 4 - hex.length(); i < len; i++) {
       sb.append('0');
     }
     sb.append(hex);
     return sb.toString();
   }
   return null;
 }
Пример #2
0
  public static String printableWithSpaces(String str) {
    if (str == null || str.length() == 0) {
      return str;
    }

    int len = str.length();
    StringBuffer sb = new StringBuffer(len + 64);
    for (int i = 0; i < len; ++i) {
      char c = str.charAt(i);
      String res = printableWithSpaces(c);
      if (res == null) {
        sb.append(c);
      } else {
        sb.append(res);
      }
    }
    return sb.toString();
  }