2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,19 @@
|
|||
var baselist = "0123456789abcdefghijklmnopqrstuvwxyz", listbase = [];
|
||||
for(var i = 0; i < baselist.length; i++) listbase[baselist[i]] = i; // Generate baselist reverse
|
||||
function basechange(snumber, frombase, tobase)
|
||||
{
|
||||
var i, t, to = new Array(Math.ceil(snumber.length * Math.log(frombase) / Math.log(tobase))), accumulator;
|
||||
if(1 < frombase < baselist.length || 1 < tobase < baselist.length) console.error("Invalid or unsupported base!");
|
||||
while(snumber[0] == baselist[0] && snumber.length > 1) snumber = snumber.substr(1); // Remove leading zeros character
|
||||
console.log("Number is", snumber, "in base", frombase, "to base", tobase, "result should be",
|
||||
parseInt(snumber, frombase).toString(tobase));
|
||||
for(i = snumber.length - 1, inexp = 1; i > -1; i--, inexp *= frombase)
|
||||
for(accumulator = listbase[snumber[i]] * inexp, t = to.length - 1; accumulator > 0 || t >= 0; t--)
|
||||
{
|
||||
accumulator += listbase[to[t] || 0];
|
||||
to[t] = baselist[(accumulator % tobase) || 0];
|
||||
accumulator = Math.floor(accumulator / tobase);
|
||||
}
|
||||
return to.join('');
|
||||
}
|
||||
console.log("Result:", basechange("zzzzzzzzzz", 36, 10));
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
// Tom Wu jsbn.js http://www-cs-students.stanford.edu/~tjw/jsbn/
|
||||
var baselist = "0123456789abcdefghijklmnopqrstuvwxyz", listbase = [];
|
||||
for(var i = 0; i < baselist.length; i++) listbase[baselist[i]] = i; // Generate baselist reverse
|
||||
function baseconvert(snumber, frombase, tobase) // String number in base X to string number in base Y, arbitrary length, base
|
||||
{
|
||||
var i, t, to, accum = new BigInteger(), inexp = new BigInteger('1', 10), tb = new BigInteger(),
|
||||
fb = new BigInteger(), tmp = new BigInteger();
|
||||
console.log("Number is", snumber, "in base", frombase, "to base", tobase, "result should be",
|
||||
frombase < 37 && tobase < 37 ? parseInt(snumber, frombase).toString(tobase) : 'too large');
|
||||
while(snumber[0] == baselist[0] && snumber.length > 1) snumber = snumber.substr(1); // Remove leading zeros
|
||||
tb.fromInt(tobase);
|
||||
fb.fromInt(frombase);
|
||||
for(i = snumber.length - 1, to = new Array(Math.ceil(snumber.length * Math.log(frombase) / Math.log(tobase))); i > -1; i--)
|
||||
{
|
||||
accum = inexp.clone();
|
||||
accum.dMultiply(listbase[snumber[i]]);
|
||||
for(t = to.length - 1; accum.compareTo(BigInteger.ZERO) > 0 || t >= 0; t--)
|
||||
{
|
||||
tmp.fromInt(listbase[to[t]] || 0);
|
||||
accum = accum.add(tmp);
|
||||
to[t] = baselist[accum.mod(tb).intValue()];
|
||||
accum = accum.divide(tb);
|
||||
}
|
||||
inexp = inexp.multiply(fb);
|
||||
}
|
||||
while(to[0] == baselist[0] && to.length > 1) to = to.slice(1); // Remove leading zeros
|
||||
return to.join('');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue