Positive integer encoding as uppercase letters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Encode positive integers as upper case (ASCII) letters | |
* Encodes 0 as A, 25 as Z, 26 as AA, 51 as AZ, 52 as BA, 701 as ZZ, 702 as AAA... and so on | |
* @param int $int | |
* @return string | |
*/ | |
function int2letters($int) { | |
$letters = ''; | |
while($int > 25) { | |
$letters = chr( $int % 26 + 65 ) . $letters; | |
$int = (int)floor($int / 26); | |
$int--; | |
} | |
$letters = chr($int + 65) . $letters; | |
return $letters; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment