如何将UTF8数字转换为书面文本

时间:2018-06-22 14:44:34

标签: c# utf-8

我正在写一个winform,将书面文本转换为 Unicode 数字和 UTF8 数字。这个位运作良好

//------------------------------------------------------------------------
        // Convert to UTF8
        // The return will be either 1 byte, 2 bytes or 3 bytes.
//-----------------------------------------------------------------------

        UTF8Encoding utf8 = new UTF8Encoding();
        StringBuilder builder = new StringBuilder();

        string utext = rchtxbx_text.Text;

        // do one char at a time
        for (int text_index = 0; text_index < utext.Length; text_index++) 
        {
            byte[] encodedBytes = utf8.GetBytes(utext.Substring(text_index, 1));

            for (int index = 0; index < encodedBytes.Length; index++)

            {
                builder.AppendFormat("{0}", Convert.ToString(encodedBytes[index], 16));
            }

            builder.Append(" ");
        }

        rchtxtbx_UTF8.SelectionFont = new System.Drawing.Font("San Serif", 20);
        rchtxtbx_UTF8.AppendText(builder.ToString() + "\r");

例如,字符乘义ש给我e4b998 e4b989 d7a9,请注意我混合使用了LtoR和RtoL文字。现在,如果用户输入数字 e4b998 ,我想向他们显示它是,采用Unicode 4E58

我尝试了一些事情,但距离我最近的是

   Encoding utf8 = Encoding.UTF8;
   rchtxbx_text.Text = Encoding.ASCII.GetString(utf8.GetBytes(e4b998));

我需要怎么做才能输入 e4b998 并将写入文本框?

2 个答案:

答案 0 :(得分:1)

类似这样的东西:

  1. source分成2个字符的块:"e4b998"-> {"e4", "b9", "98"}
  2. Convert块变成字节
  3. Encode个字节进入最终字符串

实施:

  string source = "e4b998";

  string result = Encoding.UTF8.GetString(Enumerable
    .Range(0, source.Length / 2)
    .Select(i => Convert.ToByte(source.Substring(i * 2, 2), 16))
    .ToArray());

如果您的intsource

答案 1 :(得分:0)

string s_unicode2 = System.Text.Encoding.UTF8.GetString(utf8.GetBytes(e4b998));