js 中文漢字轉(zhuǎn)Unicode、Unicode轉(zhuǎn)中文漢字、ASCII轉(zhuǎn)換Unicode、Unicode轉(zhuǎn)換ASCII、中文轉(zhuǎn)換X函數(shù)代碼
最近看不少在線工具里面都有一些編碼轉(zhuǎn)換的代碼,很多情況下我們都用得到,這里腳本之家小編就跟大家分享一下這些資料
Unicode介紹
Unicode(統(tǒng)一碼、萬國碼、單一碼)是一種在計(jì)算機(jī)上使用的字符編碼。
Unicode 是為了解決傳統(tǒng)的字符編碼方案的局限而產(chǎn)生的,它為每種語言中的每個(gè)字符設(shè)定了統(tǒng)一并且唯一的二進(jìn)制編碼,以滿足跨語言、跨平臺進(jìn)行文本轉(zhuǎn)換、處理的要求。
Unicode是國際組織制定的可以容納世界上所有文字和符號的字符編碼方案。Unicode用數(shù)字0-0x10FFFF來映射這些字符,最多可以容納1114112個(gè)字符,或者說有1114112個(gè)碼位。碼位就是可以分配給字符的數(shù)字。
Unicode 到目前為止所定義的五個(gè)平面中,第0平面(BMP)最為重要,其編碼中文漢字范圍為:4E00-9FBFCJK 統(tǒng)一表意符號 (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> //帶;號 var str="https://www.jb51.net/article/1.htm"; //不帶分號 var str2="https://www.jb51.net/article/1.htm"; 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>
這里就介紹這么多,具體的大家可以多測試一下。
相關(guān)文章
Javascript訪問html頁面的控件的方法詳細(xì)分析
這段時(shí)間在公司比較的空閑,決定研究研究javascript訪問html控件,這是很普遍的,這里我系統(tǒng)的研究javascript的訪問方式,測試通過并有下面一些研究成就,供大家分享和補(bǔ)充。2008-08-08High Performance JavaScript(高性能JavaScript)讀書筆記分析
High Performance JavaScript(高性能JavaScript)讀書筆記,讓你的js代碼更有效率。2011-05-05詳細(xì)聊聊TypeScript中unknown與any的區(qū)別
unknown類型比較謙虛,就和他本身的意思一樣,他從不禍害到其他的變量,但是any類型就是那種惡霸,屬于什么都不管,誰也不敢管的類型,這篇文章主要給大家介紹了關(guān)于TypeScript中unknown與any區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-10-10javascript拖曳互換div的位置實(shí)現(xiàn)示例
一個(gè)div拖動(dòng)互換位置的demo,還有很大優(yōu)化的空間,利用dom元素的dragstart/ondragover/ondrop事件完成,感興趣的可以了解一下2021-06-06JavaScript設(shè)計(jì)模式之建造者模式實(shí)例教程
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之建造者模式,結(jié)合實(shí)例形式分析了設(shè)計(jì)模式中建造者模式的概念、功能及JavaScript實(shí)現(xiàn)與使用建造者模式的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07JavaScript實(shí)現(xiàn)DIV層拖動(dòng)及動(dòng)態(tài)增加新層的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)DIV層拖動(dòng)及動(dòng)態(tài)增加新層的方法,設(shè)計(jì)javascript操作div層的拖動(dòng)與增加的相關(guān)技巧,需要的朋友可以參考下2015-05-05