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
|
<script type="text/javascript">
function native2ascii(strNative) {
var output = "";
for (var i=0; i<strNative.length; i++) {
var c = strNative.charAt(i);
var cc = strNative.charCodeAt(i);
if (cc > 0xff)
output += "\\u" + toHex(cc >> 8) + toHex(cc & 0xff);
else
output += c;
}
return output;
}
var hexChars = "0123456789ABCDEF";
function toHex(n) {
var nH = (n >> 4) & 0x0f;
var nL = n & 0x0f;
return hexChars.charAt(nH) + hexChars.charAt(nL);
}
function ascii2native(strAscii) {
var output = "";
var posFrom = 0;
var posTo = strAscii.indexOf("\\u", posFrom);
while (posTo >= 0) {
output += strAscii.substring(posFrom, posTo);
output += toChar(strAscii.substr(posTo, 6));
posFrom = posTo + 6;
posTo = strAscii.indexOf("\\u", posFrom);
}
output += strAscii.substr(posFrom);
return output;
}
function toChar(str) {
if (str.substr(0, 2) != "\\u") return str;
var code = 0;
for (var i=2; i<str.length; i++) {
var cc = str.charCodeAt(i);
if (cc >= 0x30 && cc <= 0x39)
cc = cc - 0x30;
else if (cc >= 0x41 && cc <= 0x5A)
cc = cc - 0x41 + 10;
else if (cc >= 0x61 && cc <= 0x7A)
cc = cc - 0x61 + 10;
code <<= 4;
code += cc;
}
if (code < 0xff) return str;
return String.fromCharCode(code);
}
</script>
写入内容,点击按钮转化
<br />
<textarea id="theText" name="theText" cols="80" rows="20" wrap="off"></textarea>
<br />
<input type="button" value="native to ascii" onclick="document.getElementById('theText').value=native2ascii(document.getElementById('theText').value);">
<input type="button" value="ascii to native" onclick="document.getElementById('theText').value=ascii2native(document.getElementById('theText').value);">
|