ITeye博客

转换Unicode (JS)

wangyu · 2009-03-23 09:19 · JavaScript · Markdown


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 + '');
}

评论(3)

  1. #3 楼 · e_soft · 2010-09-02 11:02

    hanz188 writes:

    相同的Javascript代码写了两遍?

    是不是啊?

  2. #2 楼 · hanz188 · 2010-08-31 19:07

    1、你这个转化代码与页面编码是否有关系,比如说,页面编码是UTF-8,需要将表单数据转化为GBK? 2、这个Javascript代码是不是不区分脚本语言,比如说ASP、JSP、PHP。 3、toUnicode()与toUnicode2()两个方法有什么区别?

  3. #1 楼 · hanz188 · 2010-08-31 18:46

    相同的Javascript代码写了两遍?

评论功能维护中,当前仅展示历史评论。