unicode編碼的各種表示方式,不斷更新中!

unicode編碼的各種表示方式,不斷更新中!

格式一:&#XXXX;&#XXXX;

示例:您好

結果:您好


格式二:\uXXXX\uXXXX

示例:\u60a8\u597d

結果:您好


格式三:&#DDDDD;&#DDDDD;

示例:您好

結果:您好


貼一段轉unicode編碼的JS代碼,用於轉換各種結果的方法


//convtype爲轉換類型,轉換類型爲我上文中提到的4中,code爲需要轉換的字符串
function myconf(convtype,code){ 
	switch(convtype){ 
		case "totype1": 
			return ascii(code);
		case "totype2": 
			return unicode(code);
		case "totype3": 
			return unicode1(code);
		case "tohanzi": 
			return reconvert(code);
	} 
}
function ascii(str){ 
	var value='';
	for (var i = 0; i < str.length; i++)
	    value += '\&#x' + parseInt(str.charCodeAt(i)).toString(16)+';';
	return value;
}
function unicode(str){ 
	var value='';
	for (var i = 0; i < str.length; i++)
	    value += '\\u' + parseInt(str.charCodeAt(i)).toString(16);
	return value;
}
function unicode1(str){ 
	var value='';
	for (var i = 0; i < str.length; i++)
	    value += '&#' + str.charCodeAt(i) + ';';
	return value;
}
function reconvert(str){ 
	str = str.replace(/(\\u)(\w{1,4})/gi,function($0){ 
	return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16))); 
	}); 
	str = str.replace(/(&#x)(\w{1,4});/gi,function($0){ 
	return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16)); 
	}); 
	str = str.replace(/(&#)(\d{1,6});/gi,function($0){ 
	return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2"))); 
	}); 
	return str; 
}


發佈了96 篇原創文章 · 獲贊 22 · 訪問量 58萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章