专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WPF

02、Unicode 壮汉转码小工具

发布时间:2011-06-23 13:55:48 文章来源:www.iduyao.cn 采编人员:星星草
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=""width:100%;height:100%"">" + strS + "</textarea></body></html>";

     webSource.NavigateToString(html);
 }


 // 示例数据
 string strS = @"{""code"":200,""description"":""\u7b2c\u4e8c\u5b63\u4e2d\uff0c\u7434\u5b50\u7ec8\u4e8e\u7ed3\u675f\u4e86\u5bf9\u76f4\u6811\u7684\u5355\u604b\uff0c\u4e8c\u4eba\u7ed3\u5a5a\u540e\u76f4\u6811\u8fd8\u662f\u4e00\u76f4\u4fdd\u6301\u7740\u51b7\u9759\u7684\u6027\u683c\uff0c\u4e3a\u4e86\u80fd\u5728\u672a\u6765\u7684\u4e8b\u4e1a\u4e0a\u5e2e\u4e0a\u76f4\u6811\u7684\u5fd9\uff0c\u7434\u5b50\u8fdb\u5165\u4e86\u62a4\u58eb\u5b66\u6821\u5b66\u4e60\uff0c\u540c\u65f6\u5728\u8fdb\u5165\u5927\u5b66\u5b66\u4e60\u533b\u79d1\u7684\u76f4\u6811\u9762\u524d\u51fa\u73b0\u4e86\u5f3a\u52b2\u7684\u5bf9\u624b\u8239\u6d25\u8bda\u4e00\u3002\u540c\u65f6\u5728\u7b2c\u4e00\u90e8\u4e2d\u5355\u604b\u7434\u5b50\u7684\u91d1\u4e4b\u52a9\u5728\u7eed\u7bc7\u4e2d\u4e5f\u6536\u83b7\u4e86\u81ea\u5df1\u7684\u604b\u60c5\uff0c\u4ed6\u4e0e\u6765\u81ea\u5916\u56fd\u7684\u7559\u5b66\u751f\u514b\u4e3d\u4e1d\u5f00\u59cb\u4e86\u4e00\u6bb5\u5168\u65b0\u7684\u604b\u7231\u3002}";

 

  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;
 }

 

代码链接

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: