note 96304 deleted from function.base-convert by danbrown
| From: | danbrown@php.net | Date: | Sat, 02 Jul 2011 01:45:52 +0000 |
| Subject: | note 96304 deleted from function.base-convert by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181154@lists.php.net to get a copy of this message | ||
Note Submitter: Tamas
----
Another pair of functions to convert between numbers and their uppercase alphabet representation (A,
B, ..., Z, AA, AB, ..., AZ, BA, BB, ..., BZ, CA, ..., ZZ, AAA, AAB, etc.)
<?php
/*
* Convert an integer to a string of uppercase letters (A-Z, AA-ZZ, AAA-ZZZ, etc.)
*/
function num2alpha($n)
{
for($r = ""; $n >= 0; $n = intval($n / 26) - 1)
$r = chr($n%26 + 0x41) . $r;
return $r;
}
/*
* Convert a string of uppercase letters to an integer.
*/
function alpha2num($a)
{
$l = strlen($a);
$n = 0;
for($i = 0; $i < $l; $i++)
$n = $n*26 + ord($a[$i]) - 0x40;
return $n-1;
}
?>