js 中文漢字、Unicode、ASCII互相轉換函式程式碼
Unicode介紹
Unicode(統一碼、萬國碼、單一碼)是一種在計算機上使用的字元編碼。
Unicode 是為了解決傳統的字元編碼方案的侷限而產生的,它為每種語言中的每個字元設定了統一併且唯一的二進位制編碼,以滿足跨語言、跨平臺進行文字轉換、處理的要求。
Unicode是國際組織制定的可以容納世界上所有文字和符號的字元編碼方案。Unicode用數字0-0x10FFFF來對映這些字元,最多可以容納1114112個字元,或者說有1114112個碼位。碼位就是可以分配給字元的數字。
Unicode 到目前為止所定義的五個平面中,第0平面(BMP)最為重要,其編碼中文漢字範圍為:4E00-9FBFCJK 統一表意符號 (CJK Unified Ideographs)
ASCII介紹
ASCII是基於拉丁字母的一套電腦編碼系統。它主要用於顯示現代英語和其他西歐語言。
它是現今最通用的單位元組編碼系統,並等同於國際標準ISO/IEC 646。
0-127 是7位ASCII 碼的範圍,是國際標準。至於漢字,不同的字符集用的ascii 碼的範圍也不一樣,常用的漢字字符集有GB2312-80,GBK,Big5,unicode 等。
GB_2312 字符集是目前最常用的漢字編碼標準。在這個標準中,每個漢字用2個位元組來表示,每個位元組的ascii碼為 161-254 (16 進位制A1 - FE),第一個位元組 對應於 區碼的1-94 區,第二個位元組 對應於位碼的1-94 位。
ASCII介紹
native2ascii是sun java sdk提供的一個工具。用來將別的文字類檔案(比如*.txt,*.ini,*.properties,*.java等等)編碼轉為Unicode編碼。為什麼要進行轉碼,原因在於程式的國際化。
安裝了jdk後,假如你是在windows上安裝,那麼在jdk的安裝目錄下,會有一個bin目錄,其中native2ascii.exe正是native2ascii中文轉unicode工具。
native2ascii的命令列的命名格式:native2ascii -[options] [inputfile [outputfile]]。
例如:native2ascii zh.txt u.txt:將zh.txt轉換為Unicode編碼,輸出檔案到u.txt。
本工具中漢字與Unicode轉換採用PHP開發,支援十六進位制和十進位制表示,能夠中文漢字和Unicode互轉;預設情況下采用十六進位制。
下面函式都需要用到的函式
?1 2 3 4 5 6 7 8 |
function
left_zero_4(str) {
if
(str != null
&& str != '' && str !=
'undefined' ) {
if
(str.length == 2) {
return
'00' + str;
}
}
return
str;
}
|
中文漢字轉Unicode
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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轉中文漢字、ASCII轉換Unicode
?1 2 3 4 5 6 7 8 9 10 11 12 13 |
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轉換ASCII
?1 2 3 4 5 6 |
function
unicode1(str){
var
value= '' ;
for
( var
i = 0; i < str.length; i++)
value +=
'&#' + str.charCodeAt(i) +
';' ;
return
value;
}
|
中文轉換&#XXXX
?1 2 3 4 5 6 7 |
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;
}
|
完整的可以測試的程式碼
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<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>
提供一箇中文漢字Unicode互轉、 ASCII與Unicode互轉的線上工具,方便幫助你解決中文的亂碼問題。
<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');" >中文漢字轉Unicode</button>
<button onclick= "javascript:a('RECONVERT');" >Unicode轉中文漢字</button>
<button onclick= "javascript:a('RECONVERT')" >ASCII轉換Unicode</button>
<button onclick= "javascript:a('CONVERT_FMT3');" >Unicode轉換ASCII</button>
<button onclick= "javascript:a('CONVERT_FMT1');" >中文轉換& #XXXX</button>
</div>
<textarea name= "output_area"
id= "output_area"
onclick= "this.select();"
placeholder= "處理之後的Unicode或Ascii字元"
value= "" ></textarea>
|
這裡就介紹這麼多,具體的大家可以多測試一下。
原文地址http://www.jb51.net/article/99274.htm