Here you can find the source of toUnicode(String input)
Parameter | Description |
---|---|
input | a parameter |
public static String toUnicode(String input)
//package com.java2s; public class Main { /**/* w w w.ja v a2 s . c om*/ * * @param input * @return */ public static String toUnicode(String input) { StringBuffer ret = new StringBuffer(); for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); if ((!Character.isWhitespace(ch) && ch < 0x20 || ch > 0x7e)) { ret.append("\\u"); // requires 1.5 VM // ret.append(String.format("%1$04x", new Object[] { // Integer.valueOf(ch) })); ret.append(fillChar(Integer.toHexString(ch), '0', 4, true)); } else { ret.append(ch); } } return ret.toString(); } /** * * @param sSource * @param ch * @param nLen * @param bLeft * @return */ public static String fillChar(String sSource, char ch, int nLen, boolean bLeft) { int nSrcLen = sSource.length(); if (nSrcLen <= nLen) { // left fill StringBuffer buffer = new StringBuffer(); if (bLeft) { for (int i = 0; i < (nLen - nSrcLen); i++) { buffer.append(ch); } buffer.append(sSource); } else // right fill { buffer.append(sSource); for (int i = 0; i < (nLen - nSrcLen); i++) buffer.append(ch); } return (buffer.toString()); } return sSource; } }