-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
114 lines (107 loc) · 3.22 KB
/
Copy pathindex.html
File metadata and controls
114 lines (107 loc) · 3.22 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1">
<link rel="stylesheet" type="text/css" href="../global.css">
<script src="../js/jquery.min.js"></script>
<title>字符串编码转换小工具</title>
<style>
#m-input{
height: 400px;
width: 600px;
font-size: 24px;
}
</style>
</head>
<body>
<div>
<script>
function keySend(){
if(event.keyCode == 13 && event.ctrlKey){
convertToUtf();
}
}
function ConvertToUtf8String(code){
if(code < 0x80){
return String.fromCharCode(code);
}
var utf = code;
var mask = -1<<6;
var pad = 0x80;
do{
if(utf&mask){
utf = (utf&(~mask))|((utf&mask)<<2)|((~mask)+1)<<1;
}else{
if(utf&pad){
pad |=pad>>1;
pad <<=8;
}
utf |= pad;
break;
}
mask<<=8;
pad |=pad>>1;
pad <<=8;
}while(true);
var out = [];
for(var j = 24; j >=0 ; j-=8){
var t = (utf>>j)&0xff;
if(t){
out.push(t.toString(16).toUpperCase());
}
}
return '\\x'+out.join('\\x');
}
function convertToUtf()
{
var val = $("#m-input").val();
var out = [];
for(var i = 0; i < val.length; i++){
var code = val.charCodeAt(i);
out.push(ConvertToUtf8String(code));
continue;
var utf = 0;
if(code<0x80){
utf = code;
}else{
utf = code;
var mask = -1<<6;
var pad = 0x80;
do{
if(utf&mask){
utf = (utf&(~mask))|((utf&mask)<<2)|((~mask)+1)<<1;
}else{
if(utf&pad){
pad |=pad>>1;
pad <<=8;
}
utf |= pad;
break;
}
mask<<=8;
pad |=pad>>1;
pad <<=8;
}while(true);
}
for(var j = 24; j >=0 ; j-=8){
var t = (utf>>j)&0xff;
if(t){
out.push(t.toString(16).toUpperCase());
}
}
}
$("#m-before").html(val);
$("#m-after").html(out.join(""));
}
</script>
<textarea id="m-input" onkeydown="keySend();">输入要转换的字符串</textarea>
<br/>
<button onclick="convertToUtf();">转UTF-8</button>
<h5>原文</h5>
<p id="m-before"></p>
<h5>转义字符串</h5>
<pre id="m-after" style="outline: solid red 1px"></pre>
</div>
</body>
</html>