Here you can find the source of toUnicode(String text)
public static String toUnicode(String text)
//package com.java2s; //License from project: Open Source License public class Main { public static String toUnicode(String text) { int bufLen = text.length() * 2; StringBuilder sb = new StringBuilder(bufLen); for (int i = 0; i < text.length(); i++) { char chr = (char) text.charAt(i); if ((chr > 61) && (chr < 127)) { if (chr == '\\') { sb.append('\\'); sb.append('\\'); continue; }// w w w .jav a 2 s . c o m sb.append(chr); continue; } if (isChinese(chr)) sb.append("\\u" + Integer.toHexString(text.charAt(i) & 0xffff)); else sb.append(chr); } return sb.toString(); } public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } }