• 02、Unicode 汉字转码小工具


       在做 Windows app 的时候,与服务器端交互使用的是 json 格式的数据,里面的汉字内容被

    编码成 unicode 格式,在调试的时候不太方便,就写了个工具,把里面的 unicode 内容转换成

    汉字,方便调试。这个小工具是几个月前写的了,放在公司电脑的磁盘上,在其它地方使用时,

    有点麻烦。就放到自己的博客里了。

         这个工具很简单,运行截图:

      

       1、在 xaml 页面中,放置两个 WebBrowser 控件,左侧用来显示 unicode 字符串,右侧显示转码后的结果。之所以使用浏览器控件,

    而不直接使用 TextBlock 控件(或 TextBox),是因为这些 Wpf 控件对文字的复制、粘贴操作不能直接使用。

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <WebBrowser x:Name="webSource" Grid.Column="0"/>        
       
        <Button Content="转换" Click="Button_Click" Height="100" 
                HorizontalAlignment="Center" Grid.Column="1" 
                VerticalAlignment="Top" Margin="10"/>
    
        <WebBrowser x:Name="webResult" Grid.Column="2"/>
    </Grid>

       2、在 WebBrowser 控件使用的 html 字符串中,放置一个 textarea 表单控件,并把它 css 样式 width、height 设置为 100%。

    在构造函数中,默认显示测试文本内容:

     public MainWindow()
     {
         InitializeComponent();
    
    
         // 需要指定为 utf-8 编码,否则默认为 gb-2312
         string html = @"<html><head><meta charset=""utf-8""></head><body><textarea id=""txtArea"" style=""100%;height:100%"">" + strS + "</textarea></body></html>";
    
         webSource.NavigateToString(html);
     }
    
    
     // 示例数据
     string strS = @"{""code"":200,""description"":""u7b2cu4e8cu5b63u4e2duff0cu7434u5b50u7ec8u4e8eu7ed3u675fu4e86u5bf9u76f4u6811u7684u5355u604buff0cu4e8cu4ebau7ed3u5a5au540eu76f4u6811u8fd8u662fu4e00u76f4u4fddu6301u7740u51b7u9759u7684u6027u683cuff0cu4e3au4e86u80fdu5728u672au6765u7684u4e8bu4e1au4e0au5e2eu4e0au76f4u6811u7684u5fd9uff0cu7434u5b50u8fdbu5165u4e86u62a4u58ebu5b66u6821u5b66u4e60uff0cu540cu65f6u5728u8fdbu5165u5927u5b66u5b66u4e60u533bu79d1u7684u76f4u6811u9762u524du51fau73b0u4e86u5f3au52b2u7684u5bf9u624bu8239u6d25u8bdau4e00u3002u540cu65f6u5728u7b2cu4e00u90e8u4e2du5355u604bu7434u5b50u7684u91d1u4e4bu52a9u5728u7eedu7bc7u4e2du4e5fu6536u83b7u4e86u81eau5df1u7684u604bu60c5uff0cu4ed6u4e0eu6765u81eau5916u56fdu7684u7559u5b66u751fu514bu4e3du4e1du5f00u59cbu4e86u4e00u6bb5u5168u65b0u7684u604bu7231u3002}";

      3、WebBrowser 控件有一个 Document 属性,表示的是 “所承载的 HTML 页的文档对象”。因为它是 object 类型的,

    不能直接通过该属性获得 html 的 dom 树内容(比如 o.body.innerHTML 获得 body 里面的标签内容)。 需要额外添加

    Microsoft.mshtml 程序集,并引入 mshtml.HTMLDocument 类型。

          按钮的单击事件代码:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                // 获取表示所承载的 HTML 页的文档对象。
                mshtml.HTMLDocument o = webSource.Document as mshtml.HTMLDocument; // 使用 mshtml.HTMLDocument 需要添加  Microsoft.mshtml 程序集引用
    
                // 通过 HTMLDocument 对象,直接获得 html 的dom 树内容,
                // 并把内容 转换为 汉字
                string strResult = ToChinsesWord(o.body.innerHTML);  
    
    
                // 需要指定为 utf-8 编码,否则默认为 gb-2312
                string html = @"<html><head><meta charset=""utf-8""> 
    </head><body>" +
                    strResult
                    + "</body></html>";
    
                webResult.NavigateToString(html);
            }


    4、把 unicode 字符串转换为汉字的逻辑:

     /// <summary>
     /// 将Unicode编码转换为汉字字符串
     /// </summary>
     /// <param name="str">Unicode编码字符串</param>
     /// <returns>汉字字符串</returns>
     public static string ToChinsesWord(string str)
     {
         // 使用指定的匹配选项在指定的输入字符串中搜索指定的正则表达式的所有匹配项
         MatchCollection mc = Regex.Matches(str, @"\u([w]{2})([w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
         byte[] bts = new byte[2];
    
         foreach (Match m in mc)
         {
             //  将指定样式的数字的字符串表示形式转换为它的等效 32 位有符号整数
             bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
             bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
    
             // 将指定字节数组中的所有字节解码为一个字符串
             string newWord = Encoding.Unicode.GetString(bts);
    
             str = str.Replace(m.Value, newWord);
         }
    
         return str;
     }

    代码链接

  • 相关阅读:
    python基础---内置类型
    python基础---内置常量
    python基础---内置函数
    数据增强---CutMix
    数据增强---Mixup
    机器学习笔记---评估方法
    深度学习笔记-上溢和下溢
    线性代数笔记-子空间
    tensorflow2学习笔记---模块、层和模型
    tensorflow2学习笔记---Graph和tf.function
  • 原文地址:https://www.cnblogs.com/hebeiDGL/p/4397642.html
Copyright © 2020-2023  润新知