Java Unicode Create toUnicode(String input)

Here you can find the source of toUnicode(String input)

Description

to Unicode

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static String toUnicode(String input) 

Method Source Code

//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;
    }
}

Related

  1. getUnicodeText(Object text)
  2. toUnicode(char c)
  3. toUnicode(final String toUnicode, final boolean toLowerCase)
  4. toUnicode(String input)
  5. toUnicode(String input)
  6. toUnicode(String original)
  7. toUnicode(String s)
  8. toUnicode(String source)
  9. toUnicode(String str)