My input data is an array of ASCII values:
[103, 81, 70, 72, 65, 65, 111, 66, 71, 65, 77, 69, 67, 81, 73, 65, 65, 78, 67, 117]
Each element of the above array is the ASCII value of the Base64 string: "gQFHAAoBGAMECQIAANCu"
I want to convert this Base64 string to hexadecimal bytes as : 81 01 0f 00 0a 01 18 03 04 09 03 00 00 81 6e.
My approach so far:
function Decode(bytes) {
var base64String = b2s(bytes);
var HexValue = toHex(base64String);
return HexValue;
}
function b2s(array) {
return String.fromCharCode.apply(String, array);
}
function toHex(str) {
var result = '';
for (var i = 0; i < str.length; i++) {
result += str.charCodeAt(i).toString(16);
}
return result;
}
const data = Decode([103, 81, 70, 72, 65, 65, 111, 66, 71, 65, 77, 69, 67, 81, 73, 65, 65, 78, 67, 117])
console.log(data);
The above code snippet gives me: "6751464841416f4247414d4543514941414e4375", which is a Hex string. I found some StackOverflow questions suggesting the following code snippet:
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();
}
However, the atob is not available in my implementation.
Did I miss anything in my approach? Any help will be highly appreciated. Thank you.