How do i get the decimal value of a unicode character in C#?

前端 未结 5 1156
南笙
南笙 2020-12-31 04:48

How do i get the numeric value of a unicode character in C#?

For example if tamil character (U+0B85) given, output should be 2949 (i.e. <

5条回答
  •  一个人的身影
    2020-12-31 05:05

    It's basically the same as Java. If you've got it as a char, you can just convert to int implicitly:

    char c = '\u0b85';
    
    // Implicit conversion: char is basically a 16-bit unsigned integer
    int x = c;
    Console.WriteLine(x); // Prints 2949
    

    If you've got it as part of a string, just get that single character first:

    string text = GetText();
    int x = text[2]; // Or whatever...
    

    Note that characters not in the basic multilingual plane will be represented as two UTF-16 code units. There is support in .NET for finding the full Unicode code point, but it's not simple.

提交回复
热议问题