打印
[实用程序源码及工具软件]

HotCRC内的XXEncodeToHex()源码

[复制链接]
222|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hotpower|  楼主 | 2023-9-23 10:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    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个字符
    }


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

506

粉丝