欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js 中文漢字轉(zhuǎn)Unicode、Unicode轉(zhuǎn)中文漢字、ASCII轉(zhuǎn)換Unicode、Unicode轉(zhuǎn)換ASCII、中文轉(zhuǎn)換&#XX函數(shù)代碼

 更新時(shí)間:2021年11月27日 21:50:52   投稿:mdxy-dxy  
這篇文章主要介紹了js 中文漢字轉(zhuǎn)Unicode、Unicode轉(zhuǎn)中文漢字、ASCII轉(zhuǎn)換Unicode、Unicode轉(zhuǎn)換ASCII、中文轉(zhuǎn)換&#XXXX,需要的朋友可以參考下

最近看不少在線工具里面都有一些編碼轉(zhuǎn)換的代碼,很多情況下我們都用得到,這里腳本之家小編就跟大家分享一下這些資料

Unicode介紹

Unicode(統(tǒng)一碼、萬國碼、單一碼)是一種在計(jì)算機(jī)上使用的字符編碼。
Unicode 是為了解決傳統(tǒng)的字符編碼方案的局限而產(chǎn)生的,它為每種語言中的每個(gè)字符設(shè)定了統(tǒng)一并且唯一的二進(jìn)制編碼,以滿足跨語言、跨平臺(tái)進(jìn)行文本轉(zhuǎn)換、處理的要求。
Unicode是國際組織制定的可以容納世界上所有文字和符號(hào)的字符編碼方案。Unicode用數(shù)字0-0x10FFFF來映射這些字符,最多可以容納1114112個(gè)字符,或者說有1114112個(gè)碼位。碼位就是可以分配給字符的數(shù)字。
Unicode 到目前為止所定義的五個(gè)平面中,第0平面(BMP)最為重要,其編碼中文漢字范圍為:4E00-9FBFCJK 統(tǒng)一表意符號(hào) (CJK Unified Ideographs)

ASCII介紹

ASCII是基于拉丁字母的一套電腦編碼系統(tǒng)。它主要用于顯示現(xiàn)代英語和其他西歐語言。
它是現(xiàn)今最通用的單字節(jié)編碼系統(tǒng),并等同于國際標(biāo)準(zhǔn)ISO/IEC 646。
0-127 是7位ASCII 碼的范圍,是國際標(biāo)準(zhǔn)。至于漢字,不同的字符集用的ascii 碼的范圍也不一樣,常用的漢字字符集有GB2312-80,GBK,Big5,unicode 等。
GB_2312 字符集是目前最常用的漢字編碼標(biāo)準(zhǔn)。在這個(gè)標(biāo)準(zhǔn)中,每個(gè)漢字用2個(gè)字節(jié)來表示,每個(gè)字節(jié)的ascii碼為 161-254 (16 進(jìn)制A1 - FE),第一個(gè)字節(jié) 對應(yīng)于 區(qū)碼的1-94 區(qū),第二個(gè)字節(jié) 對應(yīng)于位碼的1-94 位。

ASCII介紹

native2ascii是sun java sdk提供的一個(gè)工具。用來將別的文本類文件(比如*.txt,*.ini,*.properties,*.java等等)編碼轉(zhuǎn)為Unicode編碼。為什么要進(jìn)行轉(zhuǎn)碼,原因在于程序的國際化。
安裝了jdk后,假如你是在windows上安裝,那么在jdk的安裝目錄下,會(huì)有一個(gè)bin目錄,其中native2ascii.exe正是native2ascii中文轉(zhuǎn)unicode工具。
native2ascii的命令行的命名格式:native2ascii -[options] [inputfile [outputfile]]。
例如:native2ascii zh.txt u.txt:將zh.txt轉(zhuǎn)換為Unicode編碼,輸出文件到u.txt。

本工具中漢字與Unicode轉(zhuǎn)換采用PHP開發(fā),支持十六進(jìn)制和十進(jìn)制表示,能夠中文漢字和Unicode互轉(zhuǎn);默認(rèn)情況下采用十六進(jìn)制。

下面函數(shù)都需要用到的函數(shù)

function left_zero_4(str) {
			if (str != null && str != '' && str != 'undefined') {
				if (str.length == 2) {
					return '00' + str;
				}
			}
			return str;
		}

中文漢字轉(zhuǎn)Unicode

function unicode(str){
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\\u' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
			}
			return value;
		}
		function left_zero_4(str) {
			if (str != null && str != '' && str != 'undefined') {
				if (str.length == 2) {
					return '00' + str;
				}
			}
			return str;
		}

Unicode轉(zhuǎn)中文漢字、ASCII轉(zhuǎn)換Unicode

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; 
		}

Unicode轉(zhuǎn)換ASCII

		function unicode1(str){ 
			var value='';
			for (var i = 0; i < str.length; i++)
				value += '&#' + str.charCodeAt(i) + ';';
			return value;
		} 

中文轉(zhuǎn)換&#XXXX

		function ascii(str){ 
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\&#x' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16))+';';
			}
			return value;
		} 

完整的可以測試的代碼

		<script type="text/javascript">
		function a(pChoice){
			var inputEle = document.getElementById('input_area');
			var outputEle = document.getElementById('output_area');
			switch(pChoice){ 
				case "CONVERT_FMT1":
					outputEle.value = ascii(inputEle.value);
					break; 
				case "CONVERT_FMT2":
					outputEle.value = unicode(inputEle.value);
					break; 
				case "CONVERT_FMT3":
					outputEle.value = unicode1(inputEle.value);
					break; 
				case "RECONVERT":
					outputEle.value = reconvert(inputEle.value);
					break; 
			} 
		} 
		function ascii(str){ 
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\&#x' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16))+';';
			}
			return value;
		} 
		function unicode(str){
			var value='';
			for (var i = 0; i < str.length; i++) {
				value += '\\u' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
			}
			return value;
		}
		function left_zero_4(str) {
			if (str != null && str != '' && str != 'undefined') {
				if (str.length == 2) {
					return '00' + str;
				}
			}
			return str;
		}
		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; 
		}
		</script>

<style>
textarea {
 width: 100%;
 height: 200px;
 resize:vertical;
 border: 1px solid #CCC;
 /*border-radius:8px;*/
 padding:4px;
 box-shadow: 2px 2px 5px #d3d6da;
 -moz-box-shadow: 2px 2px 5px #d3d6da;
}
</style>
提供一個(gè)中文漢字Unicode互轉(zhuǎn)、 ASCII與Unicode互轉(zhuǎn)的在線工具,方便幫助你解決中文的亂碼問題。

	  <div class='divider'></div>
	  <textarea id="input_area" name="input_area" placeholder="貼入要處理的Unicode或Ascii字符" value="">jb51.net - 腳本之家</textarea>
			<div class='row'>
				<button onclick="javascript:a('CONVERT_FMT2');">中文漢字轉(zhuǎn)Unicode</button>
				<button onclick="javascript:a('RECONVERT');">Unicode轉(zhuǎn)中文漢字</button>		
				<button onclick="javascript:a('RECONVERT')">ASCII轉(zhuǎn)換Unicode</button>
				<button onclick="javascript:a('CONVERT_FMT3');">Unicode轉(zhuǎn)換ASCII</button>
				<button onclick="javascript:a('CONVERT_FMT1');">中文轉(zhuǎn)換&#XXXX</button>
			</div>
<textarea name="output_area" id="output_area" onclick="this.select();" placeholder="處理之后的Unicode或Ascii字符" value=""></textarea>

看到這里了腳本之家小編再為大家分享一個(gè)好用的可以將&#數(shù)字編碼轉(zhuǎn)換為字符串的方法

<script>
//帶;號(hào)
var str="&#104;&#116;&#116;&#112;&#115;&#58;&#47;&#47;&#119;&#119;&#119;&#46;&#106;&#98;&#53;&#49;&#46;&#110;&#101;&#116;&#47;&#97;&#114;&#116;&#105;&#99;&#108;&#101;&#47;&#49;&#46;&#104;&#116;&#109;";
//不帶分號(hào)
var str2="&#104&#116&#116&#112&#115&#58&#47&#47&#119&#119&#119&#46&#106&#98&#53&#49&#46&#110&#101&#116&#47&#97&#114&#116&#105&#99&#108&#101&#47&#49&#46&#104&#116&#109";
function uncode(str) {
return str.replace(/&#(x)?([^&]{1,5});?/g, function (a, b, c) {
return String.fromCharCode(parseInt(c, b ? 16 : 10));
})
}

document.write(uncode(str));
document.write("<br>");
document.write(uncode(str2));
</script>

這里就介紹這么多,具體的大家可以多測試一下。

在線Unicode/中文轉(zhuǎn)換工具

相關(guān)文章

最新評論