C#中文和UNICODE字符转换方法

 

代码
//中文转为UNICODE字符

string str = "中文";
            
string outStr = "";
            
if (!string.IsNullOrEmpty(str))
            {                
                
for (int i = 0; i < str.Length; i++)
                {
                    
//将中文字符转为10进制整数,然后转为16进制unicode字符
                    outStr += "\\u" + ((int)str[i]).ToString("x");
                }
            }



//UNICODE字符转为中文

            
string str = "\\u4e2d\\u6587";
            
string outStr = "";
            
if (!string.IsNullOrEmpty(str))
            {
                
string[] strlist = str.Replace("\\","").Split('u');
                
try
                {
                    
for (int i = 1; i < strlist.Length; i++)
                    {
                        
//将unicode字符转为10进制整数,然后转为char中文字符
                        outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
                    }
                }
                
catch (FormatException ex)
                {
                    outStr 
= ex.Message;
                }
            }
代码
C#

    
/// <summary>
    
/// 将汉字转换为Unicode
    
/// </summary>
    
/// <param name="text">要转换的字符串</param>
    
/// <returns></returns>
    public static string GBToUnicode(string text)
    {
        
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text);
        
string lowCode = "", temp = "";
        
for (int i = 0; i < bytes.Length; i++)
        {
            
if (i % 2 == 0)
            {
                temp 
= System.Convert.ToString(bytes[i], 16);//取出元素4编码内容(两位16进制)
                if (temp.Length < 2) temp = "0" + temp;
            }
            
else
            {
                
string mytemp = Convert.ToString(bytes[i], 16);
                
if (mytemp.Length < 2) mytemp = "0" + mytemp; lowCode = lowCode + @"\u" + mytemp + temp;//取出元素4编码内容(两位16进制)
            }
        }
        
return lowCode;
    }

    
/// <summary>
    
/// 将Unicode转换为汉字
    
/// </summary>
    
/// <param name="name">要转换的字符串</param>
    
/// <returns></returns>
    public string UnicodeToGB(string text)
    {
        MatchCollection mc 
= Regex.Matches(text, "([\\w]+)|(\\\\u([\\w]{4}))");
        
if (mc != null && mc.Count > 0)
        {
            StringBuilder sb 
= new StringBuilder();
            
foreach (Match m2 in mc)
            {
                
string v = m2.Value;
                
string word = v.Substring(2);
                
byte[] codes = new byte[2];
                
int code = Convert.ToInt32(word.Substring(02), 16);
                
int code2 = Convert.ToInt32(word.Substring(2), 16);
                codes[
0= (byte)code2;
                codes[
1= (byte)code;
                sb.Append(Encoding.Unicode.GetString(codes));
            }
            
return sb.ToString();
        }
        
else
        {
            
return text;
        }
    }

js

<script Language=Javascript>
var classObj
=
     {
       ToUnicode:function(str) 
       {
        
return escape(str).replace(/%/g,"\\").toLowerCase();
       },
    
       UnUnicode:function(str)
       {
        
return unescape(str.replace(/\\/g, "%"));
       },

      copyingTxt:function(str)
      {
       document.getElementById(str).select(); 
       document.execCommand(
"Copy"); 
      }
    }
</script>
<textarea id=codes style="width:500px;height:300px"></textarea><br><br>
<input type=button value=Unicode加密 onclick=javascript:codes.value=classObj.ToUnicode(codes.value)>
<input type=button value=Unicode解密 onclick=javascript:codes.value=classObj.UnUnicode(codes.value)>
<input type=button value=复制上面文本 onclick=javascript:classObj.copyingTxt("codes")>
<input type=button value=清空上面内容 onclick=javascript:codes.value="">

 


 

 

posted @ 2010-12-24 00:09  冰封的心  阅读(711)  评论(0)    收藏  举报