Getting unicode string from its code - C#

一笑奈何 提交于 2019-11-28 18:12:10
arul

You want to use the char.ConvertFromUtf32 function.

string codePoint = "0D15";

int code = int.Parse(codePoint, System.Globalization.NumberStyles.HexNumber);
string unicodeString = char.ConvertFromUtf32(code);
// unicodeString = "ക"

Here's an NUnit test showing arul and Adrian's solution - note that one solution starts with input in a string, while with the other solution the input starts in just a char.

    [Test]
    public void testConvertFromUnicode()
    {

        char myValue = Char.Parse("\u0D15");
        Assert.AreEqual(3349, myValue);

        char unicodeChar = '\u0D15';
        string unicodeString = Char.ConvertFromUtf32(unicodeChar);
        Assert.AreEqual(1, unicodeString.Length);
        char[] charsInString = unicodeString.ToCharArray();
        Assert.AreEqual(1, charsInString.Count());
        Assert.AreEqual((int) '\u0D15', charsInString[0]);
    }

Escape the character in the xml using a character reference:

<Config value="&#x0D15;" />

It will get read properly by c#'s xml parser (at least XElement.Load()).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!