Return code point of characters in C#

后端 未结 6 1294
梦谈多话
梦谈多话 2020-12-10 01:52

How can I return the Unicode Code Point of a character? For example, if the input is \"A\", then the output should be \"U+0041\". Ideally, a solution should take care of sur

6条回答
  •  青春惊慌失措
    2020-12-10 02:10

    The following code writes the codepoints of a string input to the console:

    string input = "\uD834\uDD61";
    
    for (var i = 0; i < input.Length; i += char.IsSurrogatePair(input, i) ? 2 : 1)
    {
        var codepoint = char.ConvertToUtf32(input, i);
    
        Console.WriteLine("U+{0:X4}", codepoint);
    }
    

    Output:

    U+1D161

    Since strings in .NET are UTF-16 encoded, the char values that make up the string need to be converted to UTF-32 first.

提交回复
热议问题