codice:
public class FormattaInHex {
public static void main(String[] args) throws Exception {
String str = "\uD83D\uDE00";
System.out.println("Stringa originale: " + str);
System.out.println();
System.out.println("Convertita: " + toHex(str.getBytes("UTF-8")) );
}
private static String toHex(byte[] b) {
StringBuilder ret = new StringBuilder();
int temp = 0;
for(int i=0; i<b.length; i++) {
temp = b[i];
if (temp < 0) temp += 256;
ret.append( (temp < 16) ? "0" + Integer.toHexString(temp) : Integer.toHexString(temp) );
}
return ret.toString();
}
}
L'output (sulla console di Windows, che quindi non supporta la visualizzazione dei caratteri unicode):
codice:
Stringa originale: ?
Convertita: f09f9880
Ciao.