High based number.

JavaScript and client side scripting.

Moderator: General Moderators

High based number.

Postby JellyFish » Sat Dec 29, 2007 2:05 am

How would a javascript function add one to a string as if it's a high based number; higher then base 10?

For example, if the string passed to the function was "a" then the function would return "b". If the string was "2B" then it would return "2C". If the string was "d1k3" then the function would return "d1k4".

I think you get the idea. Also note that a upper case "A" would return an upper case "B" and a lower case "z" would go to the upper case letters; return "A". After "9" it's "a" etc.

I don't really have the idea on where I would start if I were to create such a function?

Thanks for reading guys. :)

EDIT: I've found out that parseInt() function accepts a radix which means I can use it to convert a string like "a" to an integer like "10". Which is great! In other words I can use parseInt() to convert a case-insensitive string of number and alphabet characters into an integer, then from there I simple add one to that integer. Now I need a way to transform that integer back into a base 36 string.

Latest toBase() Extension:
Syntax: [ Download ] [ Hide ]
 
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;
 
Last edited by JellyFish on Wed Apr 09, 2008 5:30 pm, edited 7 times in total.
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Re: High based number.

Postby JellyFish » Sat Dec 29, 2007 4:16 am

JellyFish wrote:How would a javascript function add one to a string as if it's a high based number; higher then base 10?

For example, if the string passed to the function was "a" then the function would return "b". If the string was "2B" then it would return "2C". If the string was "d1k3" then the function would return "d1k4".

I think you get the idea. Also note that a upper case "A" would return an upper case "B" and a lower case "z" would go to the upper case letters; return "A". After "9" it's "a" etc.

I don't really have the idea on where I would start if I were to create such a function?

Thanks for reading guys. :)

EDIT: I've found out that parseInt() function accepts a radix which means I can use it to convert a string like "a" to an integer like "10". Which is great! In other words I can use parseInt() to convert a case-insensitive string of number and alphabet characters into an integer, then from there I simple add one to that integer. Now I need a way to transform that integer back into a base 36 string.


Woot! This solves the problem:

Syntax: [ Download ] [ Hide ]

(parseInt("a",36)).toBase(36);

 


As you can see I can convert the integer back to base 36. But how? Some of you may not recognize this syntax; you may never have heard of the toBase() method before.

That's because I created it!

Syntax: [ Download ] [ Hide ]

Number.prototype.toBase = function (base)

{

        var decimal = this;

        var conversion = "";

        var symbolSheet = [0,1,2,3,4,5,6,7,8,9, 'a','b','c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L', 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

        if (base > symbolSheet.length)

        {

                return false;

        }

        while (decimal >= 1)

        {

                conversion = symbolSheet[(decimal - (base*Math.floor(decimal / base)))] +conversion;

                decimal = Math.floor(decimal / base);

        }

        return conversion;

}

 


Up to base 62 conversion using the algorithm I made personally. :D

The only issue now is that parseInt() turns out to be inferior to my method I'm so proud of; parseInt() only goes up to base 32.
Last edited by JellyFish on Sun Dec 30, 2007 12:00 am, edited 1 time in total.
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Postby Kieran Huggins » Sat Dec 29, 2007 3:38 pm

JellyFish++

nice 8)
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby JellyFish » Sat Dec 29, 2007 4:57 pm

Kieran Huggins wrote:JellyFish++

nice 8)


JellyFish++? Is this some kind of recent DevNet lingo that I'm missing?

But HEY! I've written in some extra features to my method. First of all it now converts any base <= 62 to any other base lower or equal to 62. Sense base 62 cannot be represented with regular characters 0-9, this method would have to be for strings as well as numbers.

Syntax: [ Download ] [ Hide ]

Object.prototype.toBase = function (base,fromBase)

{

        var symbolSheet = [0,1,2,3,4,5,6,7,8,9, 'a','b','c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L', 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','+','/'];

        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 isNaN(conversion) ? conversion : parseInt(conversion);

}

 


Syntax: [ Download ] [ Hide ]

/*

(number|string).toBase(returnBase:number, [fromBase:number]):number|string;

-toBase returns a number or string

-returnBase: the base to convert to

-fromBase: the base to convert from

*/




//examples

(3).toBase(2); //returns 11 in base 2

(11).toBase(10,2); //reads 11 as base 2 and returns 3 in base 10

(11).toBase(16,2); //reads 11 as base 2 and returns 3 in base 16

(30).toBase(16); //returns "1e" in base 16

 


I don't know if I should utilize javascript built in hexadecimal value.
Last edited by JellyFish on Sun Dec 30, 2007 12:02 am, edited 4 times in total.
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Postby Kieran Huggins » Sat Dec 29, 2007 5:17 pm

I guess the ++ means your value just increased! Or maybe it means you gained weight. Cloning is another possible explanation.

We typically use that syntax it around here to give virtual props. It's sort of a form of un-tabulated meta-moderation if you will. It's completely useless, except that you can exchange 50 of them for one invisible pink unicorn.

On an almost completely unrelated note: http://en.wikipedia.org/wiki/Wikipedia:Barnstars

I really do like where this extension is going - I'll even award you one more "useless point" for it: JellyFish++
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby JellyFish » Sat Dec 29, 2007 5:32 pm

Kieran Huggins wrote:I guess the ++ means your value just increased! Or maybe it means you gained weight. Cloning is another possible explanation.

We typically use that syntax it around here to give virtual props. It's sort of a form of un-tabulated meta-moderation if you will. It's completely useless, except that you can exchange 50 of them for one invisible pink unicorn.

On an almost completely unrelated note: http://en.wikipedia.org/wiki/Wikipedia:Barnstars

I really do like where this extension is going - I'll even award you one more "useless point" for it: JellyFish++


Alright. :lol:

I'm kinda at a halt as to what to do next with this extension. Any ideas, or do you suggest a final release?

PS: Thanks for all the unicorns. :D
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Postby Kieran Huggins » Sat Dec 29, 2007 7:03 pm

To earn your unicorns I'd ask for base64 support, since base64 is so ubiquitous.

Adding '+' and '/' to your symbol map should do the trick, but I'm not sure what order do put them in. I'm reasonably sure that just tacking them on to the end in that order is correct.

Edit: There are padding implications as well... http://en.wikipedia.org/wiki/Base64
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby JellyFish » Sat Dec 29, 2007 7:29 pm

Kieran Huggins wrote:To earn your unicorns I'd ask for base64 support, since base64 is so ubiquitous.

Adding '+' and '/' to your symbol map should do the trick, but I'm not sure what order do put them in. I'm reasonably sure that just tacking them on to the end in that order is correct.

Edit: There are padding implications as well... http://en.wikipedia.org/wiki/Base64


Sounds good. Although my preference would be '-' and '_' sense they seem more like a space character and would interfere with adding and division characters, possibly.

I changed the last draft of my method to have 64 characters in the symbolSheet.
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Postby Kieran Huggins » Sat Dec 29, 2007 8:00 pm

the existing base64 encoding scheme uses the '+' and '/' characters, so if you want it to be compatible (which would be a real plus!) you'll have to use those. You can compare against the PHP version of base64_encode() to test your function
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby JellyFish » Sat Dec 29, 2007 8:10 pm

Kieran Huggins wrote:the existing base64 encoding scheme uses the '+' and '/' characters, so if you want it to be compatible (which would be a real plus!) you'll have to use those. You can compare against the PHP version of base64_encode() to test your function


I guess it's good. :D
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Postby Kieran Huggins » Sat Dec 29, 2007 8:19 pm

The more I think about it, the more I realize that base64 encoding is for strings and your method is for number base conversion! A completely different kettle of fish.

No worries, I am an idiot 8)
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby JellyFish » Mon Jan 07, 2008 6:33 pm

Kieran Huggins wrote:The more I think about it, the more I realize that base64 encoding is for strings and your method is for number base conversion! A completely different kettle of fish.

No worries, I am an idiot 8)


The toBase function is bound to both the Number and String objects' prototypes. So if a number is converted to base 64 the toBase function will return a string and if you where to convert it back to any other base lower or equal to 10 then the toBase function will return a number.

Syntax: [ Download ] [ Hide ]

var n = (12).toBase(64); //n = "c"

n = n.toBase(10, 64); //n = 12

 
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Postby VladSun » Mon Jan 07, 2008 6:57 pm

http://jsfromhell.com/number/base-conversor

Created: 2005.10.02


;) Google it (c) ;)
User avatar
VladSun
DevNet Master
 
Posts: 4294
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Postby JellyFish » Mon Jan 07, 2008 10:19 pm

VladSun wrote:http://jsfromhell.com/number/base-conversor

Created: 2005.10.02


;) Google it (c) ;)


Cool. It's just like mine. Almost. Except that mine is solely toBase method; he modifies the parseInt method. I like how he does it though.
User avatar
JellyFish
DevNet Resident
 
Posts: 1361
Joined: Tue Feb 14, 2006 8:18 pm
Location: San Diego, CA

Postby Kieran Huggins » Mon Jan 07, 2008 11:29 pm

his only goes up to base 62! Jelly FTW!
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Next

Return to Javascript

Who is online

Users browsing this forum: Google [Bot], qwprdo5fvu and 4 guests