]> jfr.im git - snippets.git/commitdiff
add PHP num2code and code2num
authorJohn <redacted>
Wed, 18 Jul 2018 19:25:24 +0000 (14:25 -0500)
committerGitHub <redacted>
Wed, 18 Jul 2018 19:25:24 +0000 (14:25 -0500)
snippets.php [new file with mode: 0644]

diff --git a/snippets.php b/snippets.php
new file mode 100644 (file)
index 0000000..19bfdee
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+function num2code($num, $dict) {
+       // Source: perl Acme::Tools
+       // License: GNU GPL v1+; Artistic License
+       if (!is_array($dict)) {
+               $dict = str_split($dict);
+       }
+       $base = count($dict);
+       $buf = array();
+       while ($num > 0) {
+               $buf[] = $dict[$num % $base];
+               $num = (int)($num/$base);
+               var_dump($num);
+       }
+       return implode(array_reverse($buf));
+}
+
+function code2num($str, $dict) {
+       // Source: perl Acme::Tools
+       // License: GNU GPL v1+; Artistic License
+       if (is_array($dict)) {
+               $dict = implode($dict);
+       }
+       $base = strlen($dict);
+       $buf = 0;
+       foreach (str_split($str) as $c) {
+               $buf = $buf*$base + strpos($dict, $c);
+       }
+       return $buf;
+}
+
+// vim: ts=4 nosmarttab noexpandtab