40

I know following is the way to use unicode in C#

string unicodeString = "\u0D15";

In my situation, I will not get the character code (0D15) at compile time. I get this from a XML file at runtime. I wonder how do I convert this code to unicode string? I tried the following

// will not compile as unrecognized escape sequence
string unicodeString = "\u" + codeFromXML; 

// will compile, but just concatenates u with the string got from XML file.
string unicodeString = "\\u" + codeFromXML; 

How do I handle this situation?

Any help would be great!

Nosredna
  • 83,000
  • 15
  • 95
  • 122
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184

3 Answers3

64

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 = "เด•"
weston
  • 54,145
  • 21
  • 145
  • 203
arul
  • 13,998
  • 1
  • 57
  • 77
4

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]);
    }
dplante
  • 2,445
  • 3
  • 21
  • 27
1

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()).

Jimmy
  • 5,131
  • 9
  • 55
  • 81