High based number.

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Kieran Huggins wrote:his only goes up to base 62!

Code: Select all

c = (c || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
I don't think so ;) Look at the examples.

Anyway - I'm glad I've found this site - great scripts :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Code: Select all

"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Looks like only 62 characters to me; 10+(26*2)? :?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Looks like it's custom defined ;)
Help
Methods
Number.toBase(base: Integer, [charset: String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"]): String
Encodes an integer number into string.
base
base that will be used to encode the number (from 2 to charset.length)
charset
set of characters that will be used in the encoding

String.parseInt(base: Integer, [charset: String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"]): Integer
Decodes a string into an integer number.
base
base that will be used to decode the number (from 2 to charset.length)
charset
set of characters that will be used in the decoding
Only the default value is 62 chars long ...

In Javascript

Code: Select all

function a(b)
{
	b = b || 10;
        ..............
}
is the "equivalent" of this PHP code

Code: Select all

function a($b = 10)
{
       ................
}
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: High based number.

Post by JellyFish »

I can't believe it. I come back to this old post to grab the toBase extension and I find it not working anymore.

Latest extension:

Code: Select all

var toBase = function (base,fromBase)
{
    var symbolSheet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".split("");
    if (typeof fromBase != "undefined")
    {
        var decimal = 0;
        for (var i=0; i < this.toString().length; i++)
        {
            decimal = decimal + (Math.pow(fromBase,i) * symbolSheet.toString().replace(',','','g').indexOf(this.toString().charAt(this.toString().length-(i+1))));
        }
    }
    else
    {
        var decimal = this;
    }
    var conversion = "";
    if (base > symbolSheet.length || base <= 1)
    {
        return false;
    }
    while (decimal >= 1)
    {
        conversion = symbolSheet[(decimal - (base*Math.floor(decimal / base)))] +conversion;
        decimal = Math.floor(decimal / base);
    }
    return (base<11) ? parseInt(conversion): conversion;
}
String.prototype.toBase = toBase, Number.prototype.toBase = toBase;
delete toBase;
I don't know the real problem with the code. But it's as if the statements "String.prototype.toBase = toBase, Number.prototype.toBase = toBase;" don't exist. What I mean to say is, when I try something like this:

Code: Select all

 
"a".toBase(10);
//or
(10).toBase(64);
 
I get the error:
TypeError: "a".toBase is not a function
or
TypeError: (10).toBase is not a function
Any ideas?
Post Reply