Mat Banik Mat Banik - 7 months ago 15
Java Question

ASCII to HTML-Entities Escaping in Java

I found this website with escape codes and I'm just wondering if someone has done this already so I don't have to spend couple of hours building this logic:

StringBuffer sb = new StringBuffer();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
switch (c) {
case '\u25CF': sb.append("&#9679;"); break;
case '\u25BA': sb.append("&#9658;"); break;

/*
... the rest of the hex chars literals to HTML entities
*/

default: sb.append(c); break;
}
}

Answer

These "codes" is a mere decimal representation of the unicode value of the actual character. It seems to me that something like this would work, unless you want to be very strict about which codes get converted, and which don't.

StringBuilder sb = new StringBuilder();
 int n = s.length();
 for (int i = 0; i < n; i++) {
     char c = s.charAt(i);
     if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) {
        sb.append("&#");
        sb.append((int)c);
        sb.append(';');
     } else {
        sb.append(c);
     }

 }