-
-
Save ryasmi/91d7fd30710264affeb9 to your computer and use it in GitHub Desktop.
function convertBase(value, from_base, to_base) { | |
var range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split(''); | |
var from_range = range.slice(0, from_base); | |
var to_range = range.slice(0, to_base); | |
var dec_value = value.split('').reverse().reduce(function (carry, digit, index) { | |
if (from_range.indexOf(digit) === -1) throw new Error('Invalid digit `'+digit+'` for base '+from_base+'.'); | |
return carry += from_range.indexOf(digit) * (Math.pow(from_base, index)); | |
}, 0); | |
var new_value = ''; | |
while (dec_value > 0) { | |
new_value = to_range[dec_value % to_base] + new_value; | |
dec_value = (dec_value - (dec_value % to_base)) / to_base; | |
} | |
return new_value || '0'; | |
} |
Bug
// Base 10 to Base 26, also get issue Base26 to Base10 with hight numbers ...
// my testcase, nothing to explain ...
{
expected: "DGSJENFCE",
operande: "equal",
data: { num: "897521678123", srcAlphabet: NUMERALS_10.join(""), dstAlphabet: ALPHABET_LATIN.join("") }
},
BUT HAVE "EHTKFOGDF"
"DGSJENFCE" expected because of this
and another website have the same result but with your algo
Note: typeNumAmbiToStr is only for merge number and string to string
function swapBaseRef(valueA: string, baseA: string, ToBaseB: string) {
if (baseA.length !== ToBaseB.length) { throw new RangeError("swapBaseRef: need baseA and ToBaseB lenght equal"); }
return valueA
.split("")
.map((e, i) => { return ToBaseB[baseA.indexOf(e)] })
.join("")
}
export function anyBaseToAnyBase(num: string | number, srcAlphabet: string, dstAlphabet: string) {
let fromBase = srcAlphabet.length,
toBase = dstAlphabet.length;
let from_range = LONGEST_BASE.slice(0, fromBase);
let to_range = LONGEST_BASE.slice(0, toBase).join("");
const number = swapBaseRef(typeNumAmbiToStr(num, srcAlphabet), srcAlphabet, from_range.join(""));
let dec_value = number.split("").reverse().reduce((carry, digit, index) => {
let fromIndex = from_range.indexOf(digit);
if (fromIndex === -1) { throw new Error(`Invalid digit ${digit} for base ${fromBase}.`); }
return carry + fromIndex * Math.pow(fromBase, index);
}, 0);
let new_value = "";
while (dec_value > 0) {
new_value = to_range[dec_value % toBase] + new_value;
dec_value = (dec_value - dec_value % toBase) / toBase;
}
return swapBaseRef(new_value, to_range, dstAlphabet) || dstAlphabet[0];
};
please fixed youre code @ryansmith94 it would be awesome,
it works with small numbers but not with high numbers maybe theyre an issue when divide and float number ...
I saw a package wich have the same issue ("and we go again") ...
Because of this, prefer to use toString() ... ๐
@kzar79 @Juraj-Masiar @crispy-cat @apanasara @joeshae @zakariamouhid @c0ncentus
I moved this code to https://github.com/ryansmith94/baseroo and created issue #1 today and fixed the bug with large numbers. Thanks to @zakariamouhid for the suggested fix, I utilised that and tweaked it to work with TypeScript. Apologies for my slow reply, seemed I had notifications turned off for replies until recently, so thanks to @c0ncentus for persisting with that latest comment which did come through ๐
Now supports floats ryasmi/baseroo#37
Here is some range conditions
Support of large integer using native BigInt, (see browser support)
test