HotCRC内的XXEncodeToHex()源码
function XXEncodeToHex(hexstring){
var i, hexstr;
hexstr = "";
for (i = 0; i < hexstring.length; i += 6)
{
hexstr += XXEncodeToHexStr(hexstring.substr(i, 6));
}
return hexstr;//输出4N个字符
}
function XXEncodeToHexStr(hexstr)
{
var i, len, c, a, t, x1, x2, x3, x4, encode, hexstring;
var basestr = "+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=";
len = Math.min(hexstr.length, 6);//输入3个字节
encode = hexstr.substr(0, 6);
hexstring = "";
for (i = len; i < 6; i ++)
{
encode += "0";
}
if(len & 1) len ++;
c = hextoint(encode.substr(0, 2));
a = hextoint(encode.substr(2, 2));
t = hextoint(encode.substr(4, 2));
if(len == 6) {
x1 = c >>> 2;
x2 = ((c & 0x03) << 4) | (a >>> 4);
x3 = ((a & 0x0f) << 2) | (t >>> 6);
x4 = t & 0x3f;
hexstring= inttohex(basestr.charCodeAt(x1), 2);
hexstring+= inttohex(basestr.charCodeAt(x2), 2);
hexstring+= inttohex(basestr.charCodeAt(x3), 2);
hexstring+= inttohex(basestr.charCodeAt(x4), 2);
}
else if(len == 4) {
x1 = c >>> 2;
x2 = ((c & 0x03) << 4) | (a >>> 4);
x3 = (a & 0x0f) << 2;
hexstring= inttohex(basestr.charCodeAt(x1), 2);
hexstring += inttohex(basestr.charCodeAt(x2), 2);
hexstring += inttohex(basestr.charCodeAt(x3), 2);
hexstring += inttohex(basestr.charCodeAt(64), 2);//=
}
else if(len == 2) {
x1 = c >>> 2;
x2 = (c & 0x03) << 4;
hexstring= inttohex(basestr.charCodeAt(x1), 2);
hexstring += inttohex(basestr.charCodeAt(x2), 2);
hexstring += inttohex(basestr.charCodeAt(64), 2);//=
hexstring += inttohex(basestr.charCodeAt(64), 2);//=
}
return hexstring;//输出4个字符
}
https://bbs.21ic.com/data/attachment/forum/202309/22/101124qp474qggq7ix7pjx.jpg.thumb.jpg
页:
[1]