<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 转换器

        HEX to Base64 converter for JavaScript(用于 JavaScript 的 HEX 到 Base64 转换器)

        <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 转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

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

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

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

                    • <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>

                          • <tfoot id='NOMz7'></tfoot>