code: public static String encode(String value) {
  		if(value==null) return null;
  
  		StringBuilder encoded=null;
  		int len=value.length();
  		for(int c=0;c<len;c++) {
  			char ch=value.charAt(c);
  			if(
  				(ch<' ' && ch!='\n' && ch!='\r')
  				|| ch=='\\'
  			) {
  				if(encoded==null) {
  					encoded=new StringBuilder();
  					if(c>0) encoded.append(value, 0, c);
  				}
  				if(ch=='\\') encoded.append("\\\\");
  				else if(ch=='\b') encoded.append("\\b");
  				else if(ch=='\f') encoded.append("\\f");
  				else if(ch=='\t') encoded.append("\\t");
  				else {
  					int ich=ch;
  					encoded
  						.append("\\u")
  						.append(hexChars[(ich>>>12)&15])
  						.append(hexChars[(ich>>>8)&15])
  						.append(hexChars[(ich>>>4)&15])
  						.append(hexChars[ich&15])
  					;
  				}
  			} else {
  				if(encoded!=null) encoded.append(ch);
  			}
  		}
  		return encoded==null ? value : encoded.toString();
  	}
docs: Encodes string for binary transparency over SOAP.