Home > Web Front-end > JS Tutorial > body text

UniCode transcoding problem of js (code)

不言
Release: 2018-08-18 16:46:52
Original
1680 people have browsed it

本篇文章给大家带来的内容是关于js的UniCode转码问题 (代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

<code class="language-javascript"><script type="text/javascript"><br/>        var GB2312UnicodeConverter = {<br/>            ToUnicode: function (str) {<br/>                return escape(str).toLocaleLowerCase().replace(/%u/gi, &#39;\\u&#39;);<br/>            }<br/>            , ToGB2312: function (str) {<br/>                return unescape(str.replace(/\\u/gi, &#39;%u&#39;));<br/>            }<br/>        };<br/><br/>        var str = &#39;上海&#39;, unicode;<br/>        document.write(str + &#39;<br/>&#39;);<br/>        unicode = GB2312UnicodeConverter.ToUnicode(str);<br/>        document.write(&#39;汉字转换为Unicode代码:&#39; + unicode + &#39;<br/><br/>&#39;);<br/>        document.write(&#39;Unicode代码转换为汉字:&#39; + GB2312UnicodeConverter.ToGB2312(unicode));<br/>    </script><br/></code>
Copy after login

  

<code class="language-csharp">/// <summary><br/>        /// 汉字转换为Unicode编码<br/>        /// </summary><br/>        /// <param name="str">要编码的汉字字符串</param><br/>        /// <returns>Unicode编码的的字符串</returns><br/>        public static string ToUnicode(string str)<br/>        {<br/>            byte[] bts = Encoding.Unicode.GetBytes(str);<br/>            string r = "";<br/>            for (int i = 0; i < bts.Length; i += 2) r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, &#39;0&#39;) + bts[i].ToString("x").PadLeft(2, &#39;0&#39;);<br/>            return r;<br/>        }<br/>        /// <summary><br/>        /// 将Unicode编码转换为汉字字符串<br/>        /// </summary><br/>        /// <param name="str">Unicode编码字符串</param><br/>        /// <returns>汉字字符串</returns><br/>        public static string ToGB2312(string str)<br/>        {<br/>            string r = "";<br/>            MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);<br/>            byte[] bts = new byte[2];<br/>            foreach (Match m in mc)<br/>            {<br/>                bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);<br/>                bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);<br/>                r += Encoding.Unicode.GetString(bts);<br/>            }<br/>            return r;<br/>        }<br/></code>
Copy after login

相关推荐:

js中splice方法和slice方法的解析

js中正则表达式的代码实例

The above is the detailed content of UniCode transcoding problem of js (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!