[实用程序源码及工具软件] HotCRC内的UUEncodeToHex()源码

[复制链接]
 楼主| hotpower 发表于 2023-9-23 10:16 | 显示全部楼层 |阅读模式
菜农电子淘宝:https://hotcomlock.taobao.com/ 准备出书:http://hotcomlock.com/hotcomlock.html
  1.     function UUEncodeToHex(hexstring)
  2.     {
  3.         var i, hexstr;
  4.         hexstr = "";
  5.         for (i = 0; i < hexstring.length; i += 6)
  6.         {
  7.             hexstr += UUEncodeToHexStr(hexstring.substr(i, 6));
  8.         }
  9.         return hexstr;//输出4N个字符
  10.     }
  1.     function UUEncodeToHexStr(hexstr)
  2.     {
  3.         var i, len, c, a, t, x1, x2, x3, x4, encode, hexstring;
  4.         var basestr = "";
  5.         for(i = 32;i <=96;i ++) basestr += String.fromCharCode(i);
  6.         len = Math.min(hexstr.length, 6);//输入3个字节
  7.         encode = hexstr.substr(0, 6);
  8.         hexstring = "";
  9.         for (i = len; i < 6; i ++)
  10.         {
  11.             encode += "0";
  12.         }
  13.         if(len & 1) len ++;
  14.         c = hextoint(encode.substr(0, 2));
  15.         a = hextoint(encode.substr(2, 2));
  16.         t = hextoint(encode.substr(4, 2));
  17.         if(len == 6) {
  18.             x1 = c >>> 2;
  19.             x2 = ((c & 0x03) << 4) | (a >>> 4);
  20.             x3 = ((a & 0x0f) << 2) | (t >>> 6);
  21.             x4 = t & 0x3f;
  22.             hexstring  = inttohex(basestr.charCodeAt(x1), 2);
  23.             hexstring  += inttohex(basestr.charCodeAt(x2), 2);
  24.             hexstring  += inttohex(basestr.charCodeAt(x3), 2);
  25.             hexstring  += inttohex(basestr.charCodeAt(x4), 2);
  26.         }
  27.         else if(len == 4) {
  28.             x1 = c >>> 2;
  29.             x2 = ((c & 0x03) << 4) | (a >>> 4);
  30.             x3 = (a & 0x0f) << 2;
  31.             hexstring  = inttohex(basestr.charCodeAt(x1), 2);
  32.             hexstring += inttohex(basestr.charCodeAt(x2), 2);
  33.             hexstring += inttohex(basestr.charCodeAt(x3), 2);
  34.             hexstring += inttohex(basestr.charCodeAt(64), 2);//=
  35.         }
  36.         else if(len == 2) {
  37.             x1 = c >>> 2;
  38.             x2 = (c & 0x03) << 4;
  39.             hexstring  = inttohex(basestr.charCodeAt(x1), 2);
  40.             hexstring += inttohex(basestr.charCodeAt(x2), 2);
  41.             hexstring += inttohex(basestr.charCodeAt(64), 2);//=
  42.             hexstring += inttohex(basestr.charCodeAt(64), 2);//=
  43.         }
  44.         return hexstring;//输出4个字符
  45.     }

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

1460

主题

21619

帖子

508

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

508

粉丝
快速回复 在线客服 返回列表 返回顶部