HEX to Base64 converter for JavaScript

后端 未结 5 1861
北海茫月
北海茫月 2020-12-01 09:25

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

相关标签:
5条回答
  • 2020-12-01 09:36

    If you're working in Node or using Browserify, you can use

    var base64String = Buffer.from(hexString, 'hex').toString('base64')
    

    or

    var hexString = Buffer.from(base64String, 'base64').toString('hex')
    
    0 讨论(0)
  • 2020-12-01 09:41
    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(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))
      );
    }
    
    function base64ToHex(str) {
      for (var i = 0, bin = atob(str.replace(/[ \r\n]+$/, "")), 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(" ");
    }
    
    0 讨论(0)
  • 2020-12-01 09:48

    The excellent comment by @dandavis is modified by StackOverflow, and has some weird hidden characters.

    Here it is as one liner :

    btoa("a6b580481008e60df9350de170b7e728".match(/\w{2}/g).map(function(a){return String.fromCharCode(parseInt(a, 16));} ).join(""))
    

    or :

    function hexToBase64(hexstring) {
        return btoa(hexstring.match(/\w{2}/g).map(function(a) {
            return String.fromCharCode(parseInt(a, 16));
        }).join(""));
    }
    
    hexToBase64("a6b580481008e60df9350de170b7e728");
    

    Both return :

    "prWASBAI5g35NQ3hcLfnKA=="
    

    Note that the hex string should have an even length :

    hexToBase64("00");
    // => "AA=="
    hexToBase64("000");
    // => "AA=="
    
    0 讨论(0)
  • 2020-12-01 09:48

    I liked the approach from @eric-duminil nevertheless the solution below - avoiding regex - is ~2x faster.

    Browser:

    function hexToBase64(hexStr) {
      return btoa([...hexStr].reduce((acc, _, i) =>
        acc += !(i - 1 & 1) ? String.fromCharCode(parseInt(hexStr.substring(i - 1, i + 1), 16)) : "" 
      ,""));
    }
    

    OR

    // even a bit faster
    function hexToBase64(hexStr) {
     let base64 = "";
     for(let i = 0; i < hexStr.length; i++) {
       base64 += !(i - 1 & 1) ? String.fromCharCode(parseInt(hexStr.substring(i - 1, i + 1), 16)) : ""
     }
     return btoa(base64);
    }
    

    Node:

    const base64 = Buffer.from(hexStr, 'hex').toString('base64');
    
    0 讨论(0)
  • 2020-12-01 09:50

    Large strings, no btoa

    Solution below is good for large strings - if you want to get bytes from base64 then look HERE

    function bytesArrToBase64(arr) {
      const abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // base64 alphabet
      const bin = n => n.toString(2).padStart(8,0); // convert num to 8-bit binary string
      const l = arr.length
      let result = '';
    
      for(let i=0; i<=(l-1)/3; i++) {
        let c1 = i*3+1>=l; // case when "=" is on end
        let c2 = i*3+2>=l; // case when "=" is on end
        let chunk = bin(arr[3*i]) + bin(c1? 0:arr[3*i+1]) + bin(c2? 0:arr[3*i+2]);
        let r = chunk.match(/.{1,6}/g).map((x,j)=> j==3&&c2 ? '=' :(j==2&&c1 ? '=':abc[+('0b'+x)]));  
        result += r.join('');
      }
    
      return result;
    }
    
    function hexToBytes(hexString) {
      return hexString.match(/.{1,2}/g).map(x=> +('0x'+x));
    }
    
    
    // ---------
    // TEST
    // ---------
    let hexString = "a6b580481008e60df9350de170b7e728";
    let bytes = hexToBytes(hexString);
    let base64 = bytesArrToBase64(bytes);
    
    console.log(base64);

    0 讨论(0)
提交回复
热议问题