I've been working on a custom function to convert hexadecimal to decimal in my Scratch project:
function Hex2Decimal(hex){
var deci = 0;
var num = 1;
var hexstr = String(hex);
hexstr = hexstr.toLowerCase();
var expon = 0;
for(var i = 0; i < hex.length; i++){
expon = Math.pow(16,hexstr.length - (num+1));
if(hexstr[num+1] === "a"){
deci = (10*expon)+deci;
}else if(hexstr[num-1] === "b"){
deci = (11*expon)+deci;
}else if(hexstr[num-1] === "c"){
deci = (12*expon)+deci;
}else if(hexstr[num-1] === "d"){
deci = (13*expon)+deci;
}else if(hexstr[num-1] === "e"){
deci = (14*expon)+deci;
}else if(hexstr[num-1] === "f"){
deci = (15*expon)+deci;
}else if(hexstr[num-1] != "undefined"){
deci = (Number(hexstr[num-1])*expon)+deci;
}
num = num + 1;
}
return deci;
}
However, I'm encountering an issue when inputting "BC324240." The function returns '197338148' instead of '3157410368.' Converting the result back to hex gives 'BC32424,' where the final '0' is being ignored for some reason that I'd appreciate help identifying. I also noticed that using '10' as input returns 1...