it-tools/dist/server/chunks/chunk-5f44352e.js

20 lines
721 B
JavaScript
Raw Normal View History

2024-04-28 00:48:29 +08:00
function convertBase({ value, fromBase, toBase }) {
const range = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".split("");
const fromRange = range.slice(0, fromBase);
const toRange = range.slice(0, toBase);
let decValue = value.split("").reverse().reduce((carry, digit, index) => {
if (!fromRange.includes(digit)) {
throw new Error(`Invalid digit "${digit}" for base ${fromBase}.`);
}
return carry += fromRange.indexOf(digit) * fromBase ** index;
}, 0);
let newValue = "";
while (decValue > 0) {
newValue = toRange[decValue % toBase] + newValue;
decValue = (decValue - decValue % toBase) / toBase;
}
return newValue || "0";
}
export { convertBase as c };