Convert Text To UniCode

private static String convertToUniCode(String inputString) throws Exception {
	String uniCodeString  = "";
	String prefix = "\\u";
	String unicodebyte = new String(inputString.getBytes("UTF-8"),"UTF-8");
	char[] charArray = unicodebyte.toCharArray();
	StringBuffer sb = new StringBuffer();
	for(int i=0; i<charArray.length; i++) {
		//System.out.print("\\u"+Integer.toHexString(charArray[i]));
		String hex = Integer.toHexString(charArray[i]);
		if (hex.length()==2 ) {
			hex = "00" + hex;
		} else if (hex.length()==3) {
			hex = "0" + hex;
		}
		sb.append(prefix + hex);
	}
	uniCodeString = sb.toString();
	return uniCodeString;
}

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

AddThis Social Bookmark Button

Similar Posts

Comments are closed.