Converting chinese character to Unicode

前端 未结 3 812
傲寒
傲寒 2020-12-17 04:20

Let\'s say I have a random Chinese character, 玩. I want to convert it to Unicode, which would be U+73A9. How could I do this in C#?

3条回答
  •  清歌不尽
    2020-12-17 04:47

    A bit longer example, that follows the pattern in Jon Hanna's answer:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace UnicodeDecodeConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                char c = '\u73a9';
                char[] chars = {c};
                Encoding encoding = Encoding.BigEndianUnicode;
                byte[] decodeds = encoding.GetBytes(chars);
                StringBuilder stringBuilder = new StringBuilder("U+");
                foreach (byte decoded in decodeds)
                {
                    stringBuilder.Append(decoded.ToString("x2"));
                }
                Console.WriteLine(stringBuilder);
                Console.ReadLine();
            }
        }
    }
    

    --jeroen

提交回复
热议问题