|
ASP.NET將字符串轉(zhuǎn)為區(qū)碼位編碼,或者將區(qū)碼位編碼字符串轉(zhuǎn)為對(duì)應(yīng)的字符串內(nèi)容。
數(shù)字;這種編碼其實(shí)就是將單個(gè)字符轉(zhuǎn)為對(duì)應(yīng)的區(qū)碼位(數(shù)字),然后區(qū)碼位前綴加上“”,后綴加上“;”組成,對(duì)于這種編碼的字符串,瀏覽器會(huì)自動(dòng)解析為對(duì)應(yīng)的字符。
ASP.NET字符串和編碼轉(zhuǎn)換源代碼和測試代碼如下:
using System;using System.Text.RegularExpressions;public partial class purchase_property : System.Web.UI.Page{ /// <summary> /// ASP.NET將字符串轉(zhuǎn)為16進(jìn)制區(qū)碼位編碼 /// </summary> /// <param name="s">要進(jìn)行16進(jìn)制區(qū)碼位編碼的字符串</param> /// <returns>編碼后的16進(jìn)制區(qū)碼位字符串</returns> public string StringToUnicodeCodeBit(string s) { if (string.IsNullOrEmpty(s) || s.Trim() == "") return ""; string r = ""; for (int i = 0; i < s.Length; i++) r += "" + ((int)s[i]).ToString() + ";"; return r; } public string reMatchEvaluator(Match m) { return ((char)int.Parse(m.Groups[1].Value)).ToString(); } /// <summary> /// ASP.NET將16進(jìn)制區(qū)碼位編碼轉(zhuǎn)為對(duì)應(yīng)的字符串 /// </summary> /// <param name="s">16進(jìn)制區(qū)碼位編碼的字符串</param> /// <returns>16進(jìn)制區(qū)碼位編碼的字符串對(duì)應(yīng)的字符串</returns> public string UnicodeCodeBitToString(string s) { if (string.IsNullOrEmpty(s) || s.Trim() == "") return ""; Regex rx = new Regex(@"(/d+);", RegexOptions.Compiled); return rx.Replace(s, reMatchEvaluator); } protected void Page_Load(object sender, EventArgs e) { string s = "ASP.NET區(qū)碼位字符串"; s = StringToUnicodeCodeBit(s);//轉(zhuǎn)為編碼 Response.Write(s); Response.Write("/n"); s = UnicodeCodeBitToString(s);//編碼轉(zhuǎn)為字符串 Response.Write(s); }}
Javascript版本可以參考下面:
function uncode(str) {//把編碼轉(zhuǎn)換成字符 return str.replace(/(x)?([^&]{1,5});?/g, function (a, b, c) { return String.fromCharCode(parseInt(c, b ? 16 : 10)); });}function encode(str) {//把字符轉(zhuǎn)換成編碼 var a = [], i = 0; for (; i < str.length; ) a[i] = str.charCodeAt(i++); return "" + a.join(";") + ";";}
AspNet技術(shù):Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。