Search code examples
c#unicodechar

assigning a very big hexadecimal to a char through \u or \x


char k = '\u103BD';

I get a compile time error.
I want to put the following char in my char k but it seems that the escape sequence will not accept more than 4 digits.
What should I do in this case? enter image description here


Solution

  • Character char is two bytes value, that's why it can be within [\u0000..\uFFFF] range only. If you are looking for values beyond \uFFFF, please note, that it should be represented as two characters construction, e.g. string:

    string result = "\U000103BD"; // note Capital U, 8 hex digits
    
    Console.Write(result);
    

    Output:

    𐎽
    

    If you want to work with these two characters explicitly you can use the decoding below:

    public static (char left, char right) Decode(int code) {
      if (code < 0x10000)
        throw new ArgumentOutOfRangeException(nameof(code));
    
      return (
        (char)(((code - 0x10000) >> 10) + 0xD800),
        (char)(((code - 0x10000) & 0b1111_111_111) + 0xDC00)
      );
    }
    

    Demo:

    (char left, char right) = Decode(0x103BD);
    
    Console.Write(string.Concat(left, right));
    

    Output:

    𐎽
    

    Finally, from .Net 6 on you can use Rune structure to operate with these character pairs:

    Rune rune = new Rune(0x103BD);
    
    Console.Write(rune);
    

    Output:

    𐎽