2

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 Answers2

0

You can look at someone elses implementation of atob and copy their process. E.g. https://github.com/jsdom/abab/blob/master/lib/atob.js

ZephDavies
  • 3,964
  • 2
  • 14
  • 19
-1

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"));
GMKHussain
  • 3,342
  • 1
  • 21
  • 19
  • Array.from(atob('AQ0gIAcDExUQAQECAWQW')).map(c=>c.charCodeAt(0).toString(16).padStart(2,'0')).join('').toUpperCase() // "010D20200703131510010102016416" – ZephDavies Jul 06 '20 at 11:45
  • 1
    I can't use atob because while using that it shows function is undefined.I need to use it for thingsboard platform – Sony Choudhary Jul 06 '20 at 16:31
  • javax.script.ScriptException: ReferenceError: "atob" is not defined in at line number 20 – Sony Choudhary Jul 06 '20 at 16:49