Decoder Base64 to Hex

Hello everyone,

I am currently writing a decoder for a sensor. The standart decoder from the producer uses Hex to decode.

Since my Data comes in Base64 I need a function to convert to Hex.

Sadly I coundn’t find a solution for a function. I used

function byte2HexString(bytes) {
var retHexString = “”;
bytes.forEach(function (el) {
retHexString = retHexString.concat((“0” + (Number(el).toString(16))).slice(-2).toUpperCase());
});
return retHexString;
}

But I get the error that “for Each” is not a function

I also tryed

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

Where I get the Error “atob is not defined”.

I am an Beginner for programing. If I unterstand it corretly it has to do with JavaScript “Otto”. Does anyone else had the same problem and found a solution?

Kind regards

Chris

What version are you using?

I have the same problem with chirpstack 3.

Please note that the JS engine of ChirpStack v4 is a lot more advanced compared to ChirpStack v3.

1 Like

This link might help you Base64 Decoding In JavaScript - javatpoint. Use atob or an external library. I hope it can be useful. Let me know if you need anything else.