Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Positive integer encoding as uppercase letters
/**
* 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