utf To Unicode - Android java.lang

Android examples for java.lang:String Unicode

Description

utf To Unicode

Demo Code


//package com.java2s;
import java.lang.Character.UnicodeBlock;

public class Main {
    public static String utf8ToUnicode(String source) {
        int len = source.length();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < len; i++) {
            char unicodeChar = source.charAt(i);

            UnicodeBlock ub = UnicodeBlock.of(unicodeChar);

            if (ub == UnicodeBlock.BASIC_LATIN) {
                sb.append(source);//from  w  w w.j  ava  2s .  c o  m
            } else if (ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

                int j = (int) unicodeChar - 65248;
                sb.append((char) j);

            } else {
                int s = (int) unicodeChar;

                if (s < 0)
                    s = s + 2 ^ 32;
                String hexS = Integer.toHexString(s);
                String unicode = "\\u" + hexS.toUpperCase();
                sb.append(unicode);
            }
        }
        return sb.toString();
    }
}

Related Tutorials