现在的位置: 首页 > 综合 > 正文

形如“\u3223”的U码转为汉字的三种方法

2012年12月30日 ⁄ 综合 ⁄ 共 2266字 ⁄ 字号 评论关闭
    .Net 开发的dll大多数都能通过.NET Reflector进行反编译,从而得到源代码。不过源代码中的中文都是以“\u3223”这种形式显示出来。这对分析代码是非常别扭的。下面,元宝总结了三种将这U码可以转换为汉字的方法:
1、用Visual Studio 2005宏来实现转换
下面是一断将\Uxxxx形式的字符串还原成中文的Visual Studio 2005宏,刚刚从网上看到,转录如下:
U码转汉字的VS2005的宏:
Imports EnvDTE
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Diagnostics
Public Module Birdshome
    
Sub Unicode2Character()
        
Dim doc As Document = DTE.ActiveDocument
        
Dim docText As TextDocument = doc.Object
        
Dim selText As TextSelection = docText.Selection()
        selText.SelectAll()
        
Dim text As String = selText.Text
        
Dim iLength As Integer
        
Do
            iLength 
= text.Length
            
Dim m As Match
            
Dim strPattern As String = "(?<code>\\u[A-F0-9]{4})"
            m 
= Regex.Match(text, strPattern, RegexOptions.IgnoreCase)
            
If m.Success Then
                
Dim strValue As String
                strValue 
= m.Groups("code").Value
                text 
= text.Replace(strValue, "")
                
Dim int As Integer
                
int = System.Int32.Parse(strValue.Substring(24), NumberStyles.HexNumber)
                
Dim ch As Char = ChrW(int)
                docText.ReplacePattern(strValue, ch)
            
Else
                
Exit Do
            
End If
            
If Not text.Length < iLength Then
                
Exit Do
            
End If
        
Loop
        selText.StartOfDocument()
    
End Sub

End Module

使用方法:
在vs2005里的"工具"->"宏资源管理器"->"新建模块",将宏代码复制以建立一个宏,
然后打开某个.cs文件.在新建的宏上右键点"运行",就可将当前的.cs文件的U码转为汉字.

2、MSDN下载的一个Unicode工具来实现

          Unicocde工具下载
          这个工具只能实现字串中转换,对于大量文本文件,尤其是NET Reflector生成的工程来说,还得采用下面的工具。

 

3、改进的元宝文件编码转换器

        private string U2CnCode(string str)
        
{
            Match m;
            Regex r 
= new Regex("(?<code>\\\\u[a-z0-9]{4})",RegexOptions.IgnoreCase );
            
for (m = r.Match(str); m.Success; m = m.NextMatch())
            
{
                
string strValue = m.Result("${code}");   //代码
                int CharNum = Int32.Parse(strValue.Substring(24), System.Globalization.NumberStyles.HexNumber);
                
string ch =string.Format("{0}",(char)CharNum);
                str 
= str.Replace(strValue, ch);
            }

            
return str;
        }

        元宝文件编码转换器下载
          注:  下载后添加上述代码

抱歉!评论已关闭.