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

[复制链接]
 楼主| hotpower 发表于 2023-9-22 10:11 | 显示全部楼层 |阅读模式
本帖最后由 hotpower 于 2023-9-22 14:18 编辑

菜农电子淘宝:https://hotcomlock.taobao.com/ 准备出书:http://hotcomlock.com/hotcomlock.html
  1.     function HexToBase64(hexstr)
  2.     {
  3.         var i, str, hexstrString;
  4.         hexstrString = "";
  5.         for (i = 0; i < hexstr.length; i += 8)
  6.         {
  7.             hexstrString += HexStrToBase64(hexstr.substr(i, 8));
  8.         }
  9.         return hexstrString;//输出3N个字节
  10.     }
  1.     function GetBase64Value(ch)
  2.     {
  3.         var value = 0;
  4.         if((ch >= "A") && (ch <= "Z")) {
  5.             value = ch.charCodeAt(0) - 0x41;
  6.         }
  7.         else if((ch >= "a") && (ch <= "z")) {
  8.             value = ch.charCodeAt(0) - 0x61 + 26;
  9.         }
  10.         else if((ch >= "0") && (ch <= "9")) {
  11.             value = ch.charCodeAt(0) - 0x30 + 52;
  12.         }
  13.         else if(ch == "+") {
  14.             value = 62;
  15.         }
  16.         else if(ch == "/") {
  17.             value = 63;
  18.         }
  19.         return value;
  20.     }
  1.     function HexStrToBase64(basehexstr)
  2.     {//输入4个字节
  3.         var c, a, t, x1, x2, x3, x4, str, hexstr;
  4.         str = HexToAscii(basehexstr);//4个字符
  5.         x1 = GetBase64Value(str.substr(0, 1));
  6.         x2 = GetBase64Value(str.substr(1, 1));
  7.         x3 = GetBase64Value(str.substr(2, 1));
  8.         x4 = GetBase64Value(str.substr(3, 1));
  9.         hexstr = "";
  10.         if(str.substr(3, 1) != "=") {
  11.             c = (x1 << 2) | (x2 >>> 4);
  12.             a = ((x2 & 0xf) << 4) | (x3 >>> 2);
  13.             t = ((x3 & 0x03) << 6) | x4;
  14.             hexstr = inttohex(c, 2);
  15.             hexstr += inttohex(a, 2);
  16.             hexstr += inttohex(t, 2);
  17.         }
  18.         else if(str.substr(2, 1) != "=") {
  19.             c = (x1 << 2) | (x2 >>> 4);
  20.             a = ((x2 & 0xf) << 4) | (x3 >>> 2);
  21.             hexstr = inttohex(c, 2);
  22.             hexstr += inttohex(a, 2);
  23.         }
  24.         else{
  25.             c = (x1 << 2) | (x2 >>> 4);
  26.             hexstr = inttohex(c, 2);
  27.         }
  28.         return hexstr;//输出3个字节
  29.     }


微信扫码截图.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

1460

主题

21619

帖子

508

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