首页 > 解决方案 > 以角度 6 将 Base64 转换为十六进制

问题描述

请您帮我解决将base64值转换为十六进制的问题,反之亦然。我目前正在研究 angular 6,似乎无法在任何地方找到解决方案。

标签: angulartype-conversionbase64hex

解决方案


这是您可以用于转换的功能

  hexAndBase64(strInput, conversionType) {
        if (conversionType == "64ToHex") {
            for (var i = 0, bin = atob(strInput.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
                let tmp = bin.charCodeAt(i).toString(16);
                if (tmp.length === 1) tmp = "0" + tmp;
                hex[hex.length] = tmp;
            }
            return hex.join(" ");
        }
        else if (conversionType == "hexTo64") {
            return btoa(String.fromCharCode.apply(null,
                strInput.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
        }
    }

推荐阅读