Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting unicode string from its code - C#

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!

like image 985
Navaneeth K N Avatar asked Jun 15 '09 04:06

Navaneeth K N


People also ask

How do you find the Unicode value of a string?

Get Unicode Character Code in Javachar char1 = 'ज'; int code = (int) char1; Here is definition of char from Oracle: The char data type is a single 16-bit Unicode character.

Are C strings Unicode?

It can represent all 1,114,112 Unicode characters. Most C code that deals with strings on a byte-by-byte basis still works, since UTF-8 is fully compatible with 7-bit ASCII. Characters usually require fewer than four bytes. String sort order is preserved.

What is the Unicode code for C?

Unicode Character “C” (U+0043)

How do I find Unicode characters?

To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.


2 Answers

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 = "ക" 
like image 140
arul Avatar answered Sep 21 '22 06:09

arul


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]);     } 
like image 39
dplante Avatar answered Sep 23 '22 06:09

dplante