off by one in Ox.encodeBase26

This commit is contained in:
rolux 2012-05-27 22:59:43 +02:00
parent f030596468
commit b6411d485a

View file

@ -4,14 +4,22 @@
Ox.encodeBase26 <b> Encode a number as bijective base26 Ox.encodeBase26 <b> Encode a number as bijective base26
See <a href="http://en.wikipedia.org/wiki/Bijective_numeration"> See <a href="http://en.wikipedia.org/wiki/Bijective_numeration">
Bijective numeration</a>. Bijective numeration</a>.
> Ox.encodeBase26(0)
''
> Ox.encodeBase26(1)
'A'
> Ox.encodeBase26(26)
'Z'
> Ox.encodeBase26(27)
'AA'
> Ox.encodeBase26(4461) > Ox.encodeBase26(4461)
'FOO' 'FOO'
@*/ @*/
Ox.encodeBase26 = function(number) { Ox.encodeBase26 = function(number) {
var string = ''; var string = '';
while (number) { while (number) {
string = String.fromCharCode(64 + number % 26) + string; string = String.fromCharCode(65 + (number - 1) % 26) + string;
number = parseInt(number / 26); number = Math.floor((number - 1) / 26);
} }
return string; return string;
}; };