转换Unicode (JS)


Encrypt = function() {
var F = {};
F.toUnicode = function(s) {
var res = [];
var len = s.length - 1;
while (len > -1) {
var ch = s.charCodeAt(len--);
if (!isNaN(ch)) {
res.push(ch);
}
}
res.push('');
return res.reverse().join('&#');
};
F.toUnicode2 = function(s) {
var res = [];
var len = s.length - 1;
while (len > -1) {
var ch = s.charCodeAt(len--);
if (!isNaN(ch)) {
chch = ch.toString(16);
switch (ch.length) {
case 4 : {
chch = ch;
}
break;
case 3 : {
ch = '0' + ch;
}
break;
case 2 : {
ch = '00' + ch;
}
break;
case 1 : {
ch = '000' + ch;
}
break;
default :
ch = null;
}
if (ch != null) {
res.push(ch);
}
}
}
res.push('');
return res.reverse().join('\\u');
};
F.toGBK = function(s) {
var res = [''];
if (s.indexOf('&#') === 0) {
for (var i = 1, cs = s.split('&#'), len = cs.length; i < len; i++) {
res.push(String.fromCharCode(cs[i]));
}
return res.join('');
} else if (s.indexOf('\\u') === 0) {
for (var i = 1, cs = s.split('\\u'), len = cs.length; i < len; i++) {
res.push(String.fromCharCode(parseInt(cs[i], 16)));
}
return res.join('');
}
return '';
};
return F;
}();

function toUnicode() {
window.document.getElementById('result').value = Encrypt.toUnicode(window.document.getElementById('param').value + '')
}
function toUnicode2() {
window.document.getElementById('result').value = Encrypt.toUnicode2(window.document.getElementById('param').value + '')
}
function toGBK() {
window.document.getElementById('result').value = Encrypt.toGBK(window.document.getElementById('param').value + '');
}




Encrypt = function() {
var F = {};
F.toUnicode = function(s) {
var res = [];
var len = s.length - 1;
while (len > -1) {
var ch = s.charCodeAt(len--);
if (!isNaN(ch)) {
res.push(ch);
}
}
res.push('');
return res.reverse().join('&#');
};
F.toUnicode2 = function(s) {
var res = [];
var len = s.length - 1;
while (len > -1) {
var ch = s.charCodeAt(len--);
if (!isNaN(ch)) {
chch = ch.toString(16);
switch (ch.length) {
case 4 : {
chch = ch;
}
break;
case 3 : {
ch = '0' + ch;
}
break;
case 2 : {
ch = '00' + ch;
}
break;
case 1 : {
ch = '000' + ch;
}
break;
default :
ch = null;
}
if (ch != null) {
res.push(ch);
}
}
}
res.push('');
return res.reverse().join('\\u');
};
F.toGBK = function(s) {
var res = [''];
if (s.indexOf('&#') === 0) {
for (var i = 1, cs = s.split('&#'), len = cs.length; i < len; i++) {
res.push(String.fromCharCode(cs[i]));
}
return res.join('');
} else if (s.indexOf('\\u') === 0) {
for (var i = 1, cs = s.split('\\u'), len = cs.length; i < len; i++) {
res.push(String.fromCharCode(parseInt(cs[i], 16)));
}
return res.join('');
}
return '';
};
return F;
}();

function toUnicode() {
window.document.getElementById('result').value = Encrypt.toUnicode(window.document.getElementById('param').value + '')
}
function toUnicode2() {
window.document.getElementById('result').value = Encrypt.toUnicode2(window.document.getElementById('param').value + '')
}
function toGBK() {
window.document.getElementById('result').value = Encrypt.toGBK(window.document.getElementById('param').value + '');
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值