Java source code for the UTF-8 to Java-code converter

// your UTF-8 string here, taken from args, request params, etc.
String utf = ...;

// convert the input string to a character array
char[] chars = utf.toCharArray();

StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.length; i++)
{
    int unipoint = Character.codePointAt(chars, i);
    if ((unipoint < 32) || (unipoint > 127))
    {
        StringBuilder hexString = new StringBuilder();
        for (int k = 0; k < 4; k++) // 4 times to build a 4-digit hex
        {
            hexString.insert(0, Integer.toHexString(unipoint % 16));
            unipoint = unipoint / 16;
        }
        sb.append("\\u"+hexString);
    }
    else
    {
        sb.append(chars[i]);
    }
}

// display the ASCII encoded string
System.out.println ("String s = " + sb.toString());
      

Back to the UTF-8 to java code converter ...

Syntax highlighter ©2004-2007 Alex Gorbatchev, homepage: http://code.google.com/p/syntaxhighlighter/