(@_@;)我是程序猿,我编程,我快乐,知识改变命运,技术成就梦想   oh yeah!合作VX "w6668263" 联系Email:ye583025823@126.com

JavaScript为unicode编码转换为中文

var str = "\\u6211\\u662Funicode\\u7F16\\u7801"; 

关于这样的数据转换为中文问题,常用的以下方法。

1. eval解析或new Function("'+ str +'")()

str = eval("'" + str + "'"); // "我是unicode编码"

2. unescape 解析

str = unescape(str.replace(/\u/g, "%u")); // "我是unicode编码"

 

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

解码

public string uncode(string str)  
        {  
            string outStr = "";  
            Regex reg = new Regex(@"(?i)//u([0-9a-f]{4})");  
            outStr = reg.Replace(str, delegate(Match m1)  
            {  
                return ((char)Convert.ToInt32(m1.Groups[1].Value, 16)).ToString();  
            });  
            return outStr;  
        }  

 

 

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(0, 2), 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="">

Java:
public static void main(String[] args) {
        String str = "\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd";
        //char[] charArray = str.toCharArray();
        //str = new String(charArray);
        System.out.println(str);
        System.out.print(str.equals("中华人民共和国"));
    }

 

Unicode小百科:

在计算机科学领域中,Unicode(统一码、万国码、单一码、标准万国码)是业界的一种标准,它可以使电脑得以呈现世界上数十种文字的系统。Unicode是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standard,目前第五版由Addison-Wesley Professional出版,ISBN-10: 0321480910)对外发表。Unicode包含了超过十万个字符(在2005年,Unicode的第十万个字符被采纳且认可成为标准之一)、一组可用以作为视觉参考的代码图表、一套编码方法与一组标准字符编码、一套包含了上标字、下标字等字符特性的列举等。 

 

Unicode组织(The Unicode Consortium)是由一个非营利性的机构所运作,并主导Unicode的后续发展,其目标在于:将既有的字符编码方案,以Unicode编码方案来加以取代,特别是既有的方案在多语环境下,皆仅有有限的空间以及不相容的问题。 

Unicode在字符集认可的成功,使其得以在电脑软件的国际化与本地化领域中,广泛且具优势的被采用。这标准已在近年来的多种新科技当中被加以采用,包含了可扩展置标语言(XML)、Java编程语言、以及最新的操作系统中。 

unicode编码的编码规则

 

比如要把“杨”编码,我们可以新建一个记事本,输入“杨”保存时选择存为unicode编码,然后查看文件二进制内容,前面的FF FE两个字节是unicode编码文件头标志,接着的68 67两个字节就是“杨”的unicode编码,用计算器换算为十进制就是26472,现在可以在一个html文件里面写入“杨”,IE打开就显示一个“杨”字。 

 
当然,对于一般ASCII码,unicode编码与ASCII编码一致,所以A可以显示一个大写字母“A”. 

posted on 2014-11-28 15:47  一个草率的龙果果  阅读(48098)  评论(4编辑  收藏  举报

导航