Page 1 of 2
High based number.
Posted: Sat Dec 29, 2007 1:05 am
by JellyFish
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:
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;
Re: High based number.
Posted: Sat Dec 29, 2007 3:16 am
by JellyFish
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:
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!
Code: Select all
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.
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.
Posted: Sat Dec 29, 2007 2:38 pm
by Kieran Huggins
JellyFish++
nice

Posted: Sat Dec 29, 2007 3:57 pm
by JellyFish
Kieran Huggins wrote:JellyFish++
nice

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.
Code: Select all
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);
}
Code: Select all
/*
(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.
Posted: Sat Dec 29, 2007 4:17 pm
by Kieran Huggins
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++
Posted: Sat Dec 29, 2007 4:32 pm
by JellyFish
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.
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.

Posted: Sat Dec 29, 2007 6:03 pm
by Kieran Huggins
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
Posted: Sat Dec 29, 2007 6:29 pm
by JellyFish
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.
Posted: Sat Dec 29, 2007 7:00 pm
by Kieran Huggins
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
Posted: Sat Dec 29, 2007 7:10 pm
by JellyFish
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.

Posted: Sat Dec 29, 2007 7:19 pm
by Kieran Huggins
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

Posted: Mon Jan 07, 2008 5:33 pm
by JellyFish
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

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.
Code: Select all
var n = (12).toBase(64); //n = "c"
n = n.toBase(10, 64); //n = 12
Posted: Mon Jan 07, 2008 5:57 pm
by VladSun
Posted: Mon Jan 07, 2008 9:19 pm
by JellyFish
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.
Posted: Mon Jan 07, 2008 10:29 pm
by Kieran Huggins
his only goes up to base 62! Jelly FTW!