Issue
I have a string like this:
"مرحبا هذا نص تجريبي"
And, I want to convert it to something like this:
"\u0623\u0646\u0627"
Solution
String input = "مرحبا هذا نص تجريبي";
StringBuilder buf = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch >= 32 && ch < 127)
buf.append(ch);
else
buf.append(String.format("\\u%04x", (int) ch));
}
String result = buf.toString();
System.out.println(result);
Output
\u0645\u0631\u062d\u0628\u0627 \u0647\u0630\u0627 \u0646\u0635 \u062a\u062c\u0631\u064a\u0628\u064a
Answered By - Andreas
Answer Checked By - David Goodson (JavaFixing Volunteer)