Here you can find the source of convertStringToUnicodeString(String s)
Parameter | Description |
---|---|
s | Java String to convert to unicode |
public static String convertStringToUnicodeString(String s) throws Exception
//package com.java2s; // The MIT License public class Main { /**//from w w w . jav a2 s.c o m * Converts a regular java string to its unicode string representation<br><br> * * @param s Java String to convert to unicode * @return the converted string */ public static String convertStringToUnicodeString(String s) throws Exception { StringBuffer convert = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if ((byte) ch >= 0 && (int) ch <= 255) convert.append(ch); else { if ((int) ch > 255 || (int) ch < 0) throw new Exception("Invalid Character: " + ch); String val = "\\u"; int intVal = (int) ch; int bit4 = (int) (intVal / Math.pow(16, 3)); intVal -= bit4 * Math.pow(16, 3); int bit3 = (int) (intVal / Math.pow(16, 2)); intVal -= bit3 * Math.pow(16, 2); int bit2 = (int) (intVal / Math.pow(16, 1)); intVal -= bit2 * Math.pow(16, 1); int bit1 = (int) (intVal / Math.pow(16, 0)); intVal -= bit1 * Math.pow(16, 0); if (intVal != 0) System.err.println("Oops, no character to conver to..."); convert.append(val + Character.toUpperCase(Integer.toHexString(bit4).charAt(0)) + Character.toUpperCase(Integer.toHexString(bit3).charAt(0)) + Character.toUpperCase(Integer.toHexString(bit2).charAt(0)) + Character.toUpperCase(Integer.toHexString(bit1).charAt(0))); } // else } // for return convert.toString(); } }