<i id='MjfZy'><tr id='MjfZy'><dt id='MjfZy'><q id='MjfZy'><span id='MjfZy'><b id='MjfZy'><form id='MjfZy'><ins id='MjfZy'></ins><ul id='MjfZy'></ul><sub id='MjfZy'></sub></form><legend id='MjfZy'></legend><bdo id='MjfZy'><pre id='MjfZy'><center id='MjfZy'></center></pre></bdo></b><th id='MjfZy'></th></span></q></dt></tr></i><div id='MjfZy'><tfoot id='MjfZy'></tfoot><dl id='MjfZy'><fieldset id='MjfZy'></fieldset></dl></div>

<small id='MjfZy'></small><noframes id='MjfZy'>

<tfoot id='MjfZy'></tfoot>
    <bdo id='MjfZy'></bdo><ul id='MjfZy'></ul>

<legend id='MjfZy'><style id='MjfZy'><dir id='MjfZy'><q id='MjfZy'></q></dir></style></legend>

        用于 JavaScript 的 HEX 到 Base64 转换器

        时间:2023-10-01

        <small id='U6HRn'></small><noframes id='U6HRn'>

          <tbody id='U6HRn'></tbody>
        <legend id='U6HRn'><style id='U6HRn'><dir id='U6HRn'><q id='U6HRn'></q></dir></style></legend>
        <tfoot id='U6HRn'></tfoot>

          <i id='U6HRn'><tr id='U6HRn'><dt id='U6HRn'><q id='U6HRn'><span id='U6HRn'><b id='U6HRn'><form id='U6HRn'><ins id='U6HRn'></ins><ul id='U6HRn'></ul><sub id='U6HRn'></sub></form><legend id='U6HRn'></legend><bdo id='U6HRn'><pre id='U6HRn'><center id='U6HRn'></center></pre></bdo></b><th id='U6HRn'></th></span></q></dt></tr></i><div id='U6HRn'><tfoot id='U6HRn'></tfoot><dl id='U6HRn'><fieldset id='U6HRn'></fieldset></dl></div>

                • <bdo id='U6HRn'></bdo><ul id='U6HRn'></ul>
                  本文介绍了用于 JavaScript 的 HEX 到 Base64 转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  任何人都知道将 HEX 编码字符串转换为 base64 编码字符串的 JavaScript 代码片段吗?

                  Anyone know of a good snippet of JavaScript code to convert HEX encoded strings to base64 encoded strings?

                  推荐答案

                  if (!window.atob) {
                    var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                    var table = tableStr.split("");
                  
                    window.atob = function (base64) {
                      if (/(=[^=]+|={3,})$/.test(base64)) throw new Error("String contains an invalid character");
                      base64 = base64.replace(/=/g, "");
                      var n = base64.length & 3;
                      if (n === 1) throw new Error("String contains an invalid character");
                      for (var i = 0, j = 0, len = base64.length / 4, bin = []; i < len; ++i) {
                        var a = tableStr.indexOf(base64[j++] || "A"), b = tableStr.indexOf(base64[j++] || "A");
                        var c = tableStr.indexOf(base64[j++] || "A"), d = tableStr.indexOf(base64[j++] || "A");
                        if ((a | b | c | d) < 0) throw new Error("String contains an invalid character");
                        bin[bin.length] = ((a << 2) | (b >> 4)) & 255;
                        bin[bin.length] = ((b << 4) | (c >> 2)) & 255;
                        bin[bin.length] = ((c << 6) | d) & 255;
                      };
                      return String.fromCharCode.apply(null, bin).substr(0, bin.length + n - 4);
                    };
                  
                    window.btoa = function (bin) {
                      for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
                        var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
                        if ((a | b | c) > 255) throw new Error("String contains an invalid character");
                        base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
                                                (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
                                                (isNaN(b + c) ? "=" : table[c & 63]);
                      }
                      return base64.join("");
                    };
                  
                  }
                  
                  function hexToBase64(str) {
                    return btoa(String.fromCharCode.apply(null,
                      str.replace(/
                  |
                  /g, "").replace(/([da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))
                    );
                  }
                  
                  function base64ToHex(str) {
                    for (var i = 0, bin = atob(str.replace(/[ 
                  ]+$/, "")), hex = []; i < bin.length; ++i) {
                      var tmp = bin.charCodeAt(i).toString(16);
                      if (tmp.length === 1) tmp = "0" + tmp;
                      hex[hex.length] = tmp;
                    }
                    return hex.join(" ");
                  }
                  

                  这篇关于用于 JavaScript 的 HEX 到 Base64 转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Node.js 中的安全随机令牌 下一篇:将 matplotlib png 转换为 base64 以便在 html 模板中查看

                  相关文章

                    <small id='NOMz7'></small><noframes id='NOMz7'>

                    <legend id='NOMz7'><style id='NOMz7'><dir id='NOMz7'><q id='NOMz7'></q></dir></style></legend>

                      <bdo id='NOMz7'></bdo><ul id='NOMz7'></ul>

                  1. <i id='NOMz7'><tr id='NOMz7'><dt id='NOMz7'><q id='NOMz7'><span id='NOMz7'><b id='NOMz7'><form id='NOMz7'><ins id='NOMz7'></ins><ul id='NOMz7'></ul><sub id='NOMz7'></sub></form><legend id='NOMz7'></legend><bdo id='NOMz7'><pre id='NOMz7'><center id='NOMz7'></center></pre></bdo></b><th id='NOMz7'></th></span></q></dt></tr></i><div id='NOMz7'><tfoot id='NOMz7'></tfoot><dl id='NOMz7'><fieldset id='NOMz7'></fieldset></dl></div>
                    1. <tfoot id='NOMz7'></tfoot>