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)制編碼,以滿足跨語言、跨平臺(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="https://www.jb51.net/article/1.htm"; //不帶分號(hào) 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>
這里就介紹這么多,具體的大家可以多測試一下。
- JS實(shí)現(xiàn)漢字與Unicode碼相互轉(zhuǎn)換的方法詳解
- JS將unicode碼轉(zhuǎn)中文方法
- Json_encode防止?jié)h字轉(zhuǎn)義成unicode的方法
- js unicode 編碼解析關(guān)于數(shù)據(jù)轉(zhuǎn)換為中文的兩種方法
- 如何讓Jackson JSON生成的數(shù)據(jù)包含的中文以unicode方式編碼
- javascript unicode與GBK2312(中文)編碼轉(zhuǎn)換方法
- 無語,javascript居然支持中文(unicode)編程!
- 解決JSON.stringify()自動(dòng)將中文轉(zhuǎn)譯成unicode的問題
- JS實(shí)現(xiàn)的漢字與Unicode碼相互轉(zhuǎn)化功能分析
相關(guān)文章
JavaScript下一版本標(biāo)準(zhǔn)ES6的Set集合使用詳解
ES6:全稱ECMAScript 6.0,是JavaScript語言的國際標(biāo)準(zhǔn),JavaScript是ECMAScript的實(shí)現(xiàn)。今天我們就來學(xué)習(xí)一下ES6的Set集合的使用2023-02-02使用PBFunc在Powerbuilder中支付寶當(dāng)面付款功能
這篇文章主要介紹了使用PBFunc在Powerbuilder中支付寶當(dāng)面付款功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10JavaScript中undefined和null的區(qū)別
這篇文章主要介紹了 JavaScript中undefined和null的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-05-05js實(shí)現(xiàn)拖拽 閉包函數(shù)詳細(xì)介紹
在開發(fā)過程中可能會(huì)使用js實(shí)現(xiàn)拖拽等相關(guān)功能,本文將以此問題進(jìn)行深入介紹,需要了解的朋友可以參考下2012-11-11JS函數(shù)式編程之純函數(shù)、柯里化以及組合函數(shù)
這篇文章主要介紹了JS函數(shù)式編程之純函數(shù)、柯里化以及組合函數(shù),文章對三個(gè)函數(shù)進(jìn)行分析講解,內(nèi)容也很容易理解,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01Three.js使用OrbitControls后修改相機(jī)旋轉(zhuǎn)方向無效解決辦法
three.js是用javascript寫的基于webGL的第三方3D庫,下面這篇文章主要給大家介紹了關(guān)于Three.js使用OrbitControls后修改相機(jī)旋轉(zhuǎn)方向無效的解決辦法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01JavaScript實(shí)現(xiàn)輪播圖方法(邏輯清晰一看就懂)
這篇文章主要給大家介紹了關(guān)于JavaScript實(shí)現(xiàn)輪播圖方法的相關(guān)資料,JS輪播圖的實(shí)現(xiàn)核心是使用JavaScript來控制圖片的切換和顯示,配合HTML和CSS完成布局和樣式設(shè)置,文中介紹的方法邏輯清晰一看就懂,需要的朋友可以參考下2023-12-12JavaScript自動(dòng)內(nèi)存管理與垃圾回收策略詳細(xì)分析講解
JS的垃圾回收機(jī)制是為了以防內(nèi)存泄漏,內(nèi)存泄漏的含義就是當(dāng)已經(jīng)不需要某塊內(nèi)存時(shí)這塊內(nèi)存還存在著,垃圾回收機(jī)制就是間歇的不定期的尋找到不再使用的變量,并釋放掉它們所指向的內(nèi)存。因?yàn)閮?nèi)存的大小是有限的,所以當(dāng)內(nèi)存不再需要的時(shí)候,我們需要對其進(jìn)行釋放2023-01-01javascript實(shí)現(xiàn)tabs選項(xiàng)卡切換效果(擴(kuò)展版)
常用的頁面效果有彈出層效果,無縫滾動(dòng)效果,選項(xiàng)卡切換效果,接下來與大家分享一款自己用原生javascript寫的選項(xiàng)卡切換效果在原有的基礎(chǔ)上進(jìn)行了擴(kuò)展,加入了自動(dòng)輪播,這樣就變成了類似圖片輪播的效果2013-03-03