3

How can I convert string with special chars, like this:
małoletni => ma\u0142oletni
Where ł converts into \u0142 (the same with rest special chars).

I've tried with

System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();

but it returns strange results.

How can I convert it?

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
whoah
  • 4,363
  • 10
  • 51
  • 81

1 Answers1

0
var str = "małoletni\nPi(π)";

//ESCAPE
var escaped = String.Join("", str.Select(c => c>31 && c < 128 ? c.ToString() : "\\u" + ((int)c).ToString("x4")));

//UNESCAPE
var unescaped = Regex.Unescape(escaped)
EZI
  • 15,209
  • 2
  • 27
  • 33