Decode Base64 to Hexadecimal string with javascript

前端 未结 5 1195
有刺的猬
有刺的猬 2020-12-06 09:52

Needing to convert a Base64 string to Hexadecimal with javascript.

Example:

var base64Value = \"oAAABTUAAg==\"

Need conversion m

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 10:40

    atob() then charCodeAt() will give you binary & toString(16) will give you hex.

    function base64ToHex(str) {
      const raw = atob(str);
      let result = '';
      for (let i = 0; i < raw.length; i++) {
        const hex = raw.charCodeAt(i).toString(16);
        result += (hex.length === 2 ? hex : '0' + hex);
      }
      return result.toUpperCase();
    }
    
    console.log(base64ToHex("oAAABTUAAg=="));

提交回复
热议问题