博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Unicode转汉字的实现

Posted on 2007-06-01 16:35  单摆  阅读(2354)  评论(4编辑  收藏  举报
private string UnicodeToGB(string content)
        
{
            Regex objRegex 
= new Regex("&#(?<UnicodeCode>[\\d]{5});", RegexOptions.IgnoreCase);
            Match objMatch 
= objRegex.Match(content);
            StringBuilder sb 
= new StringBuilder(content) ;
            
while(objMatch.Success)
            
{
                
string code = Convert.ToString(Convert.ToInt32(objMatch.Result("${UnicodeCode}")),16);
                
byte[]   array  = new  byte[2];   
                array[
0]   =   (byte)Convert.ToInt32(code.Substring(2),16);   
                array[
1]   =   (byte)Convert.ToInt32(code.Substring(0,2),16); 

                sb.Replace(objMatch.Value,Encoding.Unicode.GetString(array));

                objMatch
=objMatch.NextMatch();
            }

            
return sb.ToString();
        }