Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: converting number to alphabet and vice versa

So I have this function:

function toAlpha($data){     $alphabet =   array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');     $alpha_flip = array_flip($alphabet);     if($data <= 25){       return $alphabet[$data];     }     elseif($data > 25){       $dividend = ($data + 1);       $alpha = '';       $modulo;       while ($dividend > 0){         $modulo = ($dividend - 1) % 26;         $alpha = $alphabet[$modulo] . $alpha;         $dividend = floor((($dividend - $modulo) / 26));       }        return $alpha;     } } 

which given a number converts it into character and it works fine

but then I also want a reverse function of this that given any output of this function, return the exact input that was put in to produce that output and I tried this:

function toNum($data){ $alphabet =   array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');     $alpha_flip = array_flip($alphabet);   if(strlen($data) == 1){           return (isset($alpha_flip[$data]) ? $alpha_flip[$data] : FALSE);         }         else if(strlen($data) > 1){           $num = 1;           for($i = 0; $i < strlen($data); $i++){             if(($i + 1) < strlen($data)){               $num *= (26 * ($alpha_flip[$data[$i]] + 1));             }             else{               $num += ($alpha_flip[$data[$i]] + 1);             }           }           return ($num + 25);         } } 

but it's not working properly...toAlpha(728) is producing 'aba' but toNum('aba') is producing 1378 rather than 728...

what did I do wrong? how can I fix the reverse function so that it works properly?

thanks in advance!

like image 297
pillarOfLight Avatar asked Oct 05 '11 15:10

pillarOfLight


People also ask

How do you convert numbers into alphabets?

The Letter-to-Number Cipher (or Number-to-Letter Cipher or numbered alphabet) consists in replacing each letter by its position in the alphabet , for example A=1, B=2, Z=26, hence its over name A1Z26 .

How do I convert a number to a string in PHP?

PHP | strval() Function The strval() function is an inbuilt function in PHP and is used to convert any scalar value (string, integer, or double) to a string.


2 Answers

Shortest way, in PHP >= 4.1.0

$alphabet = range('A', 'Z');  echo $alphabet[3]; // returns D  echo array_search('D', $alphabet); // returns 3 
like image 166
Cyril Avatar answered Sep 22 '22 23:09

Cyril


I don't understand at all the logic you're trying to use in that function. What you're trying to do seems very strange (why does 'a' map to zero and yet 'aa' maps to 26?), but this appears to work. (You will want to use some more test cases, I only checked that it gives the correct output for the case 'aba'.)

function toNum($data) {     $alphabet = array( 'a', 'b', 'c', 'd', 'e',                        'f', 'g', 'h', 'i', 'j',                        'k', 'l', 'm', 'n', 'o',                        'p', 'q', 'r', 's', 't',                        'u', 'v', 'w', 'x', 'y',                        'z'                        );     $alpha_flip = array_flip($alphabet);     $return_value = -1;     $length = strlen($data);     for ($i = 0; $i < $length; $i++) {         $return_value +=             ($alpha_flip[$data[$i]] + 1) * pow(26, ($length - $i - 1));     }     return $return_value; } 
like image 22
Hammerite Avatar answered Sep 22 '22 23:09

Hammerite