import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class SmartUnicode {
private static  SmartUnicode uniCode=null;

 /**
  * 根据传入的字符构造unicode 码
  * @param ch
  * @return
  */
 public static String decode(char ch) {
  char[] chs = new char[] { ch };
  String str = new String(chs);
  try {
   byte[] bytes = str.getBytes("unicode");
   int i = bytes[3];//取unicode的高位数
   if (i < 0) {
    i = 256 + i;
   }
   String tohexStr = Integer.toHexString(i);//将字符串转换为16进制数
   i = bytes[2];
   if (i < 0) {
    i = 256 + i;
   }
   String tohexStr2 = Integer.toHexString(i);//将字符串转换为16进制数
   return "\\u" + tohexStr + tohexStr2;
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return "";

 }

 /**
  * 将字符串转换字节数组
  * @param str
  * @return
  */
 public static String decodeStr(String str) {
  char[] chars = str.toCharArray();
  String result = "";
  for (int i = 0; i < chars.length; i++) {
   result = result + uniCode.decode(chars[i]);
  }
  return result;
 }
 /**
  * 将中文转换为unicode码
  * @param str
  * @return
  */
 public static String native2ascii(String str){
  int len = str.getBytes().length;
  if (len == str.length() * 2) { //纯中文字符集  如:中国  
   return uniCode.decodeStr(str);   
  } else {  //中文与字符混合  如:中ch1国
   String result = "";
   for (int pos = 0; pos < str.length(); pos++) {
    String substr = str.substring(pos, pos + 1);
    if (substr.length()*2 == substr.getBytes().length) {
     result = result + uniCode.decodeStr(substr);
    } else
     result = result + substr;
   }   
   return result;   
  }
 } 
 
 public static void main(String[] args) throws IOException {
  SmartUnicode smd = new SmartUnicode(); 
  smd.native2ascii("产品s@ds^1f世界");
 }
}