简体   繁体   English

将中文字符转换为Unicode

[英]Converting chinese character to Unicode

Let's say I have a random Chinese character, 玩. 假设我有一个随机的中文字符,玩。 I want to convert it to Unicode, which would be U+73A9. 我想将它转换为Unicode,即U + 73A9。 How could I do this in C#? 我怎么能在C#中做到这一点?

Take myChar as a char referencing your special character... myChar作为引用你的特殊角色的字符......

Console.WriteLine("{0} U+{1:x4} {2}", myChar, (int)myChar, (int)myChar);

Above we're outputting the character itself followed by the Unicode code point and then the integer value. 上面我们输出字符本身,然后是Unicode代码点,然后输出整数值。

Reduce the format string and parameters to output only the "U+..." code... 减少格式字符串和参数,只输出“U + ...”代码...

Console.WriteLine("U+{0:x4}", (int)myChar);

The characater 玩 is in Unicode. characater玩用Unicode。

If you have it in C# as 玩, then it's currently in UTF-16, which is one of the Unicode encoding forms. 如果你用C#作为玩具,它当前是UTF-16,这是Unicode编码形式之一。

If you are obtaining it from somewhere else you need to: 如果您从其他地方获得它,您需要:

  1. Find the encoding it is in. 找到它所在的编码。
  2. Get the bytes (wrapped by a stream is nice). 获取字节(由流包裹很好)。
  3. Get of write an appropriate Encoder. 得到一个合适的编码器。
  4. Use the encoder to get the string (wrapping the nice stream with a textreader is nicer). 使用编码器获取字符串(使用文本阅读器包装好的流更好)。

Step 3 May be simple (oh, I just use that one!) or hard (darn, have to write it myself!) or somewhere in between (hey, anyone written one of these already?!) 第3步可能很简单(哦,我只是使用那个!)或者很难(darn,必须自己写!)或介于两者之间的某个地方(嘿,有人写过其中一个吗?!)

A bit longer example, that follows the pattern in Jon Hanna's answer: 更长一点的例子,遵循Jon Hanna的答案中的模式:

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 --jeroen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM