-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexEncoderAndDecoder.html
More file actions
320 lines (280 loc) · 9.28 KB
/
Copy pathHexEncoderAndDecoder.html
File metadata and controls
320 lines (280 loc) · 9.28 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HEX编码/解码工具 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.converter-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.converter-section {
display: flex;
flex-direction: column;
}
.converter-section textarea {
flex: 1;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.stats-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 15px 0;
}
.stat-item {
padding: 10px;
background-color: var(--bg-secondary);
border-radius: var(--border-radius);
text-align: center;
}
.stat-value {
font-size: 1.5em;
font-weight: bold;
color: var(--accent-color);
}
.stat-label {
font-size: 0.9em;
color: var(--text-secondary);
}
@media (max-width: 768px) {
.converter-container {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>HEX编码/解码工具</h2>
<p>将文本转换为十六进制编码或将十六进制编码解码为原文本</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• HEX编码:将文本转换为十六进制表示形式</p>
<p>• HEX解码:将十六进制编码还原为原始文本</p>
<p>• 十六进制编码常用于数据传输和存储</p>
</div>
<div class="stats-container">
<div class="stat-item">
<div class="stat-value">{{ inputLength }}</div>
<div class="stat-label">输入长度</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ outputLength }}</div>
<div class="stat-label">输出长度</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ encodingType }}</div>
<div class="stat-label">当前操作</div>
</div>
</div>
<div class="converter-container">
<div class="converter-section">
<h3>输入文本</h3>
<textarea
v-model="inputText"
placeholder="在此输入要编码或解码的文本..."
rows="10"
></textarea>
</div>
<div class="converter-section">
<h3>输出结果</h3>
<textarea
v-model="outputText"
placeholder="编码或解码结果将在这里显示"
rows="10"
readonly
></textarea>
</div>
</div>
<div class="controls">
<button @click="encodeToHex">文本 → HEX</button>
<button @click="decodeFromHex">HEX → 文本</button>
<button @click="copyResult">复制结果</button>
<button @click="swap">交换</button>
<button @click="clearAll">清空</button>
</div>
<div class="result" v-if="result">
{{ result }}
</div>
<div class="result" v-if="error" style="color: #ff6b6b;">
{{ error }}
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const inputText = ref('');
const outputText = ref('');
const result = ref('');
const error = ref('');
const encodingType = ref('待操作');
// 计算属性:统计信息
const inputLength = computed(() => inputText.value.length);
const outputLength = computed(() => outputText.value.length);
// 将文本编码为十六进制
const encodeToHex = () => {
error.value = '';
result.value = '';
encodingType.value = '编码中';
try {
if (!inputText.value) {
outputText.value = '';
result.value = '输入为空,输出也为空';
return;
}
// 将字符串转换为UTF-8字节数组,然后转为十六进制
const encoder = new TextEncoder();
const bytes = encoder.encode(inputText.value);
const hexArray = Array.from(bytes).map(byte => byte.toString(16).padStart(2, '0'));
outputText.value = hexArray.join('');
result.value = `成功将 ${inputText.value.length} 个字符编码为 ${outputText.value.length} 个十六进制字符`;
} catch (e) {
error.value = `编码错误: ${e.message}`;
}
};
// 将十六进制解码为文本
const decodeFromHex = () => {
error.value = '';
result.value = '';
encodingType.value = '解码中';
try {
if (!outputText.value) {
inputText.value = '';
result.value = '输入为空,输出也为空';
return;
}
// 验证十六进制字符串
if (!/^[0-9a-fA-F]*$/.test(outputText.value)) {
throw new Error('输入包含非十六进制字符');
}
// 如果十六进制字符串长度为奇数,补0
let hexString = outputText.value;
if (hexString.length % 2 !== 0) {
hexString = '0' + hexString;
}
// 将十六进制字符串转换为字节数组
const bytes = new Uint8Array(hexString.match(/.{2}/g).map(byte => parseInt(byte, 16)));
// 尝试解码为文本
const decoder = new TextDecoder();
inputText.value = decoder.decode(bytes);
result.value = `成功将 ${outputText.value.length} 个十六进制字符解码为 ${inputText.value.length} 个字符`;
} catch (e) {
error.value = `解码错误: ${e.message}`;
}
};
// 复制结果到剪贴板
const copyResult = () => {
if (outputText.value) {
navigator.clipboard.writeText(outputText.value)
.then(() => {
result.value = '结果已复制到剪贴板!';
})
.catch(err => {
console.error('复制失败: ', err);
error.value = '复制失败';
});
} else {
error.value = '没有可复制的内容';
}
};
// 交换输入和输出
const swap = () => {
const temp = inputText.value;
inputText.value = outputText.value;
outputText.value = temp;
result.value = '输入和输出已交换';
};
// 清空所有内容
const clearAll = () => {
inputText.value = '';
outputText.value = '';
result.value = '已清空所有内容';
error.value = '';
encodingType.value = '已清空';
};
return {
inputText,
outputText,
result,
error,
encodingType,
inputLength,
outputLength,
encodeToHex,
decodeFromHex,
copyResult,
swap,
clearAll
};
}
}).mount('#app');
</script>
</body>
</html>