Open nunnly opened 9 years ago
function Converter(){
this.toAscii = function (hex) {
var a=hex.match(/[0-9a-f]{2}/g);
for(var i=0;i<a.length;i++){
a[i]=parseInt(a[i],16)
}
return String.fromCharCode.apply(String,a);
};
this.toHex = function(ascii){
var a=[];
for(var i=0;i<ascii.length;i++){
a.push(ascii.charCodeAt(i).toString(16))
}
return a.join('');
};
}
function Converter() {
this.toAscii= function (hex) {
var i;
return [].map.call(hex, function (v, k) {
if (k % 2 === 0) {
i = v;
} else {
v = '0x' + i + v;
return String.fromCharCode(v);
}
}).join('');
},
this.toHex= function (ascii) {
return [].map.call(ascii, function (v, k) {
return v.charCodeAt(0).toString(16);
}).join('');
}
};
function Converter(){
this.toAscii = function (asContents) {
var len1 = asContents.length;
var temp = "";
for (var i = 0; i < len1; i++) {
var varasc = asContents.charCodeAt(i);
temp += parseInt(varasc,10).toString(16);
}
return temp;
};
this.toHex = function(asContents){ var temp = ""; var a=asContents.match(/[0-9a-f]{2}/g); for(var i=0;i<a.length;i++){ temp +=String.fromCharCode(parseInt(a[i],16)) } return temp; }; }
function Converter(){
this.toAscii = function (hex) {
var s="";
for(var i = 0; i < hex.length; i+=2){
s += String.fromCharCode(parseInt(hex.substr(i,2),16));
}
return s;
};
this.toHex = function(ascii){
var s="";
for(var i = 0; i < ascii.length; i++){
s += ascii.charCodeAt(i).toString(16);
}
return s;
};
}
var abc = new Converter();
var hex = abc.toHex("Look mom, no hands");
console.log(hex)
var str = abc.toAscii(hex)
console.log(str)
function Converter() {
this.toAscii = function(hex) {
return hex.split("").reduce(function(res, ch) {
return (res[1].length < 1 ? (res[1] += ch) : (res[0] += String.fromCharCode(parseInt(res[1] + ch, 16)), res[1] = "")), res;
}, ["", ""])[0];
};
this.toHex = function(ascii) {
return ascii.split("").reduce(function(res, ch) {
return res + (ch.charCodeAt(0).toString(16));
}, "");
};
}
以及了。
function Converter(){this.toAscii=function(a){return a.split("").reduce(function(a,b){return a[1].length<1?a[1]+=b:(a[0]+=String.fromCharCode(parseInt(a[1]+b,16)),a[1]=""),a},["",""])[0]},this.toHex=function(a){return a.split("").reduce(function(a,b){return a+b.charCodeAt(0).toString(16)},"")}}
递归版本
function Converter(){
this.toHex = function (string) {
if(string.length <= 1){
return string.charCodeAt(0).toString(16);
}
var mid = string.length / 2;
return this.toHex(string.slice(0,mid)) +'' + this.toHex(string.slice(mid,string.length));
};
this.toAscii = function(string){
if(string.length <= 2){
return String.fromCharCode(parseInt(string,16));
}
var mid = string.length / 2;
mid = (mid % 2 == 0 ? mid : mid+1);
return this.toAscii(string.slice(0,mid)) +'' + this.toAscii(string.slice(mid,string.length));
};
}
支持中文
function Converter(){
this.toAscii = function (hex) {
var s="";
for(var i = 0; i < hex.length; i+=4){
s += String.fromCharCode(parseInt(hex.substr(i,4),16));
}
return s;
};
this.toHex = function(ascii){
var s="",tmp;
for(var i = 0; i < ascii.length; i++){
tmp = ascii.charCodeAt(i).toString(16);
s += tmp.length==2?"00"+tmp:tmp;
}
return s;
};
}
var abc = new Converter();
var hex = abc.toHex("我喜欢你,no code say bird!");
console.log(hex)
var str = abc.toAscii(hex)
console.log(str)
写一个模块转换器,可以把传入的_ASCII_数据转换成_十六进制_的数据,也可以传入_十六进制_的数据转换成ASCII