【问题标题】:Convert Base64 to Hex in angular 6以角度 6 将 Base64 转换为十六进制
【发布时间】:2020-06-23 07:25:24
【问题描述】:

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

【问题讨论】:

    标签: angular type-conversion base64 hex


    【解决方案1】:

    这是你可以用来转换的函数

      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(" ")));
            }
        }
    

    【讨论】:

    • 我收到此错误“无法在 'Window' 上执行 'atob':要解码的字符串未正确编码。”
    • 一切正常,我在这里创建了演示应用程序 (stackblitz.com/edit/angular-ef4cxx),请检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2018-03-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多