0

I'm developing the android application that reads book from JSON format.In order to create such type of books i needed the desktop application due to comfortableness and i chose C#.


First of all i want to say that in my native language there are lots of chars that should be encoded in Unicode not in ASCII for example... [ə ç ş ğ ö ü and so on]


My problem is that there is problem with Json for some char formats and i should use the instance of this chars.(Unicode instance).For instance:

string text = "asdsdas";
text = ConvertToUnicode(Text);//->/u231/u213/u123...

i tried many ways to achieve this in JavaScript but i couldn't. Now devs please help me to solve this problem in C#.Thanks in advance any suggestion would be okay for me :).

Tərlan Əhəd
  • 179
  • 3
  • 10
  • 2
    JSON can be Unicode (UTF-8/16) so there should be no need to escape anything beyond the standard set of JSON reserved characters. If you are having problems using Unicode then its an issue with how you are encoding/transmitting the data. – Alex K. Sep 30 '17 at 14:47
  • I believe you should read http://joelonsoftware.com/articles/Unicode.html. – GSerg Sep 30 '17 at 14:48
  • Men, i guess you didn't understand me properly...Sorry but i want to convert : Hello to -> U+0048 U+0065 U+006C U+006C U+006F could you understand? – Tərlan Əhəd Sep 30 '17 at 14:51
  • 2
    Yes, but the thing is you don't need to, `{"hçi": "əçşğöü"}` is perfectly valid JSON. Failing that see [Convert a Unicode string to an escaped ASCII string](https://stackoverflow.com/questions/1615559/convert-a-unicode-string-to-an-escaped-ascii-string). – Alex K. Sep 30 '17 at 14:54
  • Okay sorry, could some one help me to solve issue, that i'm experiencing about one week, it is about android, if someone knows my Twitter account is https://mobile.twitter.com/home?locale=ru, coz my questions are not suitable for here it is more demanding :/ – Tərlan Əhəd Sep 30 '17 at 17:55
  • 1
    I think the answer you accepted does answer your stated question. But as @AlexK. points out you shouldn't need to do unless some part of your system is broken. So this becomes an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Tom Blodget Sep 30 '17 at 18:44

1 Answers1

0

You can define an extension method:

public static class Extension {
    public static string ToUnicodeString(this string str) {
        StringBuilder sb = new StringBuilder();
        foreach(var c in str) {
            sb.Append("\\u" + ((int) c).ToString("X4"));
        }
        return sb.ToString();
    }
}

which can be called like myString.ToUnicodeString()

Check it in this demo.

xGeo
  • 2,149
  • 2
  • 18
  • 39