本帖最后由 hotpower 于 2023-9-23 09:54 编辑
菜农电子淘宝:https://hotcomlock.taobao.com/ 准备出书:http://hotcomlock.com/hotcomlock.html
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 Base64ToHexStr(hexstr)
{
var i, len, c, a, t, x1, x2, x3, x4, encode, hexstring;
var basestr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
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个字符
}
|