Unicode-to-string conversion in C#

前端 未结 7 946
你的背包
你的背包 2020-12-20 17:22

How can I convert a Unicode value to its equivalent string?

For example, I have \"రమెశ్\", and I need a function that accepts this Unicode value and returns a string

7条回答
  •  遥遥无期
    2020-12-20 18:21

    Wrote a cycle for converting unicode symbols in string to UTF8 letters:

    string stringWithUnicodeSymbols = @"{""id"": 10440119, ""photo"": 10945418, ""first_name"": ""\u0415\u0432\u0433\u0435\u043d\u0438\u0439""}";
    var splitted = Regex.Split(stringWithUnicodeSymbols, @"\\u([a-fA-F\d]{4})");
    string outString = "";
    foreach (var s in splitted)
    {
        try
        {
            if (s.Length == 4)
            {
                var decoded = ((char) Convert.ToUInt16(s, 16)).ToString();
                outString += decoded;
            }
            else
            {
                outString += s;
            }
        }
        catch (Exception e)
        {
            outString += s;
        }
    }
    

提交回复
热议问题