Переведите целое число в буквы и наоборот (например, 0 = «A», 26 = «AA», 27 = «AB»). ⇐ Php
-
Гость
Переведите целое число в буквы и наоборот (например, 0 = «A», 26 = «AA», 27 = «AB»).
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){ $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?
Источник: https://stackoverflow.com/questions/766 ... 6-aa-27-ab
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){ $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?
Источник: https://stackoverflow.com/questions/766 ... 6-aa-27-ab
Мобильная версия