菜农电子淘宝:https://hotcomlock.taobao.com/ 准备出书:http://hotcomlock.com/hotcomlock.html
function HexToXXEncode(hexstr)
{
var i, str, hexstrString;
hexstrString = "";
for (i = 0; i < hexstr.length; i += 8)
{
hexstrString += HexStrToXXEncode(hexstr.substr(i, 8));
}
return hexstrString;//输出3N个字节
}
function GetXXEncodeValue(ch)
{
var value = 0;
if((ch >= "A") && (ch <= "Z")) {
value = ch.charCodeAt(0) - 0x41 + 12;
}
else if((ch >= "a") && (ch <= "z")) {
value = ch.charCodeAt(0) - 0x61 + 12 + 26;
}
else if((ch >= "0") && (ch <= "9")) {
value = ch.charCodeAt(0) - 0x30 + 2;
}
else if(ch == "+") {
value = 0;
}
else if(ch == "-") {
value = 1;
}
return value;
}
function Base64ToHex(hexstring)
{
var i, hexstr;
hexstr = "";
for (i = 0; i < hexstring.length; i += 6)
{
hexstr += Base64ToHexStr(hexstring.substr(i, 6));
}
return hexstr;//输出4N个字符
}
function HexStrToXXEncode(basehexstr)
{//输入4个字节
var c, a, t, x1, x2, x3, x4, str, hexstr;
str = HexToAscii(basehexstr);//4个字符
x1 = GetXXEncodeValue(str.substr(0, 1));
x2 = GetXXEncodeValue(str.substr(1, 1));
x3 = GetXXEncodeValue(str.substr(2, 1));
x4 = GetXXEncodeValue(str.substr(3, 1));
hexstr = "";
if(str.substr(3, 1) != "=") {
c = (x1 << 2) | (x2 >>> 4);
a = ((x2 & 0xf) << 4) | (x3 >>> 2);
t = ((x3 & 0x03) << 6) | x4;
hexstr = inttohex(c, 2);
hexstr += inttohex(a, 2);
hexstr += inttohex(t, 2);
}
else if(str.substr(2, 1) != "=") {
c = (x1 << 2) | (x2 >>> 4);
a = ((x2 & 0xf) << 4) | (x3 >>> 2);
hexstr = inttohex(c, 2);
hexstr += inttohex(a, 2);
}
else {
c = (x1 << 2) | (x2 >>> 4);
hexstr = inttohex(c, 2);
}
return hexstr;//输出3个字节
}
|