How to convert base64 to hex string without using builtin function in JavaScript

Viewed 1276

I want to convert a base64 value i.e: AQ0gIAcDExUQAQECAWQW to a hexadecimal string 010D20200703131510010102016416 using JavaScript in thingsboard. While using atob function i am getting this error javax.script.ScriptException: ReferenceError: "atob" is not defined in at line number 24

2 Answers

Why not using atob ?

function base64ToHexFunc(str) {
  const encodedData = atob(str);
  let result = '';
  for (let i = 0; i < encodedData.length; i++) {
    const hex = encodedData.charCodeAt(i).toString(16);
    result += (hex.length === 2 ? hex : '0' + hex);
  }
  return result.toUpperCase();
}

console.log(base64ToHexFunc("AQ0gIAcDExUQAQECAWQW"));

Related