Skip to content

Commit e117aff

Browse files
committed
feat: unicode 编解码
1 parent ac2b55a commit e117aff

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/pages/unicode.tsx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import MainContent from '@/components/MainContent';
2+
import TextFieldWithClean from '@/components/TextFieldWithClean';
3+
import SendIcon from '@mui/icons-material/Send';
4+
import TranslateIcon from '@mui/icons-material/Translate';
5+
import { Box, Button, Stack } from '@mui/material';
6+
import { useState } from 'react';
7+
8+
function UnicodeConverter() {
9+
const [input, setInput] = useState(''); // 输入的普通文本或unicode码
10+
const [output, setOutput] = useState(''); // 输出的unicode码或普通文本
11+
12+
// Unicode编码函数
13+
const encodeToUnicode = (str: string) => {
14+
let unicode_str = '';
15+
for (let i = 0; i < str.length; i++) {
16+
let unicode_code = str.charCodeAt(i).toString(16).toUpperCase();
17+
unicode_str += '\\u' + ('0000' + unicode_code).slice(-4);
18+
}
19+
setOutput(unicode_str);
20+
};
21+
22+
// Unicode解码函数
23+
const decodeFromUnicode = (unicode_str: string) => {
24+
let str = '';
25+
unicode_str.replace(
26+
/\\u([\da-f]{4})/gi,
27+
(match, key) => (str += String.fromCharCode(parseInt(key, 16)))
28+
);
29+
setOutput(str);
30+
};
31+
32+
// 渲染组件
33+
return (
34+
<MainContent>
35+
<Stack spacing={2} direction='row' alignItems='center'>
36+
<TextFieldWithClean
37+
multiline
38+
rows={8}
39+
value={input}
40+
variant='outlined'
41+
onClean={() => setInput('')}
42+
onChange={(e) => setInput(e.target.value)}
43+
/>
44+
<Box sx={{ minWidth: '88px' }}>
45+
<Button
46+
variant='contained'
47+
startIcon={<SendIcon />}
48+
sx={{ mb: 2 }}
49+
onClick={() => encodeToUnicode(input)}
50+
>
51+
转码
52+
</Button>
53+
<Button
54+
variant='contained'
55+
startIcon={<TranslateIcon />}
56+
onClick={() => decodeFromUnicode(input)}
57+
>
58+
解码
59+
</Button>
60+
</Box>
61+
<TextFieldWithClean
62+
multiline
63+
rows={8}
64+
value={output}
65+
onClean={() => setOutput('')}
66+
onChange={(e) => setOutput(e.target.value)}
67+
variant={'outlined'}
68+
/>
69+
</Stack>
70+
</MainContent>
71+
);
72+
}
73+
74+
export default UnicodeConverter;

src/utils/tools.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,4 +614,12 @@ export const allTools: Tool[] = [
614614
subTitle:
615615
'所谓栅栏密码,就是把要加密的明文分成N个一组,然后把每组的第1个字连起来,形成一段无规律的话。 不过栅栏密码本身有一个潜规则,就是组成栅栏的字母一般不会太多。(一般不超过30个,也就是一、两句话)',
616616
},
617+
{
618+
label: 'Unicode 编码解码',
619+
tags: [Tags.ENCODE],
620+
path: '/unicode',
621+
key: [],
622+
subTitle:
623+
'Unicode,又译作万国码、统一字元码、统一字符编码,是信息技术领域的业界标准,其整理、编码了世界上大部分的文字系统,使得电脑能以通用划一的字符集来处理和显示文字,不但减轻在不同编码系统间切换和转换的困扰,更提供了一种跨平台的乱码问题解决方案。',
624+
},
617625
];

0 commit comments

Comments
 (0)