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

HotCRC内的HexToXXEncode()源码

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


使用特权

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

本版积分规则

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

1460

主题

21619

帖子

506

粉丝