note 74780 deleted from function.chr by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Sep 2010 21:09:11 +0000 |
| Subject: | note 74780 deleted from function.chr by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172589@lists.php.net to get a copy of this message | ||
Note Submitter: vdklah at hotmail dot com
----
I'm sorry to say that the posted convertIntToAlphabet function does not work correctly. It
fails on numbers with a "Z" in it! Aside that it also gives a PHP warning, I really did
not like recursion 'cauz huge numbers leads into unneeded memory usage. Here some results
showing problems:
Dec convertIntToAlphabet
...
24 X
25 Y
26 -> empty!
27 AA
28 AB
...
76 BX
77 BY
78 -> empty!
79 CA
80 CB
...
1350 AYX
1351 AYY
1352 -> empty!
1353 A -> bad!
1354 B -> bad!
So I have written another version:
function toAlphaNumber( $num )
{
$anum = '';
while( $num >= 1 ) {
$num = $num - 1;
$anum = chr(($num % 26)+65).$anum;
$num = $num / 26;
}
return $anum;
}
With improved results:
Dec toAlphaNumber
...
24 X
25 Y
26 Z
27 AA
28 AB
...
76 BX
77 BY
78 BZ
79 CA
80 CB
...
1350 AYX
1351 AYY
1352 AYZ
1353 AZA
1354 AZB