unicode编码相互转换加密解密

需求:把字符串转换成unicode编码加密。

也可以把unicode编码解密并分析出汉字字母数字字符各多少个。

unicode编码 \u 后面是一个16进制编码,必要时需要进行转换。

看源码:

0<!DOCTYPE html>
1<html lang="en">
2<head>
3<meta charset="UTF-8">
4<title>Document</title>
5<style>
6*{
7margin: 0;
8padding: 0;
9}
10body{
11height: 5000px;
12}
13p{
14color: green;
15font-size: 20px;
16}
17</style>
18</head>
19<body>
20<input type="text">
21<button class="jm">加密</button>
22<button class="dm">解密</button>
23<p></p>
24<script>
25var ipt = document.querySelector('input'),
26jm = document.querySelector('.jm'),
27dm =document.querySelector('.dm'),
28p = document.querySelector('p');
29jm.addEventListener('click', function(){
30var iptVal = ipt.value,
31arr = [],
32iptLength = iptVal.length
33var i = 0;
34for(i; i < iptLength; i += 1){
35arr[i] = ('00' + iptVal.charCodeAt(i).toString(16)).slice(-4);
36}
37var str =  '\\u' + arr.join('\\u');
38p.innerHTML = str;
39})
40 
41dm.addEventListener('click',function(){
42var iptVal = ipt.value,
43i = 0,
44str = iptVal.replace(/\\/g,'%');
45str = unescape(str),
46strLength = str.length,
47num = 0,
48zi = 0,
49mu = 0,
50qi = 0,
51Rnum = /[0-9]/,
52Rzi = /[\u4e00-\u9fa5]/,
53Rmu = /[A-Za-z]/;
54 
55for(i; i < strLength; i += 1){
56if(Rnum.test(str[i])){
57num ++;
58}else if (Rzi.test(str[i])) {
59zi ++;
60}else if (Rmu.test(str[i])) {
61mu ++;
62}else{
63qi ++;
64}
65}
66 
67p.innerHTML = '字符串总长度:' + strLength + ',字母' + mu + '个,汉字' + zi + '个,数字' + num + '个,其他的有' + qi + '个。';
68})
69</script>
70</body>
71</html>

 

原文链接-摘自大公爵

转载于:https://www.cnblogs.com/webhb/p/5755636.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值