Using Unicode Codes in Java

Using Unicode Codes in Java

To avoid issues with encoding special characters, it is better to use Unicode codes in your Java classes instead of accented letters. Since I always need to search for this on Google, I’ve listed the most commonly used codes below to make it easier:

Character Unicode Code
á\u00e1
à\u00e0
â\u00e2
ã\u00e3
ä\u00e4
Á\u00c1
À\u00c0
Â\u00c2
Ã\u00c3
Ä\u00c4
é\u00e9
è\u00e8
ê\u00ea
É\u00c9
È\u00c8
Ê\u00ca
Ë\u00cb
í\u00ed
ì\u00ec
î\u00ee
ï\u00ef
Í\u00cd
Ì\u00cc
Î\u00ce
Ï\u00cf
ó\u00f3
ò\u00f2
ô\u00f4
õ\u00f5
ö\u00f6
Ó\u00d3
Ò\u00d2
Ô\u00d4
Õ\u00d5
Ö\u00d6
ú\u00fa
ù\u00f9
û\u00fb
ü\u00fc
Ú\u00da
Ù\u00d9
Û\u00db
ç\u00e7
Ç\u00c7
ñ\u00f1
Ñ\u00d1
&\u0026
'\u0027

To insert the Unicode code in a String, simply replace the character with the code. Example:

String s = "Diret\u00f3rio Home";

The method I used to generate this table is shown below:


public static String geraCodigoUnicode(char letra) {
    String hexa = Integer.toHexString((int) letra);

    String prefix;
    if (hexa.length() == 1) {
        prefix = "\\u000";
    } else if (hexa.length() == 2) {
        prefix = "\\u00";
    } else if (hexa.length() == 3) {
        prefix = "\\u0";
    } else {
        prefix = "\\u";
    }

    return prefix + hexa;
}



That's all folks!
This is a repost of my old blog Diretorio Home

Postar um comentário

0 Comentários