Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert a string to number and back to string?
    text
    copied!<p>I would like to know how I can convert a short ASCII string to a number (int, float, or numeric string). I saw a couple of <a href="https://stackoverflow.com/questions/3864185/string-to-number-and-back-algorithm">posts here</a> mentioned <a href="http://en.wikipedia.org/wiki/Perfect_hash_function" rel="nofollow noreferrer">perfect hashes</a> which seems like it <a href="https://stackoverflow.com/questions/2994921/perfect-hash-function-for-urls/3011311#3011311">might be what I need</a>. However, I'm not quite understanding the math for this.</p> <p><strong>How could you convert an ASCII string into a sequence of numbers and then back to a string?</strong></p> <p>As a side note, breaking a string down into it's ASCII character numbers is easy enough.</p> <pre><code>foreach(str_split($string) as $char) $number .= ord($char); </code></pre> <h3>Update</h3> <p>After more reading I came up with this. However, I'm wondering if there are anyways to <strong>shorten the number sequence</strong> so it's not quite as long.</p> <pre><code>class intnum { public static $charset = array( 32 =&gt; ' ', 33 =&gt; '!', 34 =&gt; '"', 35 =&gt; '#', 36 =&gt; '$', 37 =&gt; '%', 38 =&gt; '&amp;', 39 =&gt; "'", 40 =&gt; '(', 41 =&gt; ')', 42 =&gt; '*', 43 =&gt; '+', 44 =&gt; ',', 45 =&gt; '-', 46 =&gt; '.', 47 =&gt; '/', 48 =&gt; '0', 49 =&gt; '1', 50 =&gt; '2', 51 =&gt; '3', 52 =&gt; '4', 53 =&gt; '5', 54 =&gt; '6', 55 =&gt; '7', 56 =&gt; '8', 57 =&gt; '9', 58 =&gt; ':', 59 =&gt; ';', 60 =&gt; '&lt;', 61 =&gt; '=', 62 =&gt; '&gt;', 63 =&gt; '?', 64 =&gt; '@', 65 =&gt; 'A', 66 =&gt; 'B', 67 =&gt; 'C', 68 =&gt; 'D', 69 =&gt; 'E', 70 =&gt; 'F', 71 =&gt; 'G', 72 =&gt; 'H', 73 =&gt; 'I', 74 =&gt; 'J', 75 =&gt; 'K', 76 =&gt; 'L', 77 =&gt; 'M', 78 =&gt; 'N', 79 =&gt; 'O', 80 =&gt; 'P', 81 =&gt; 'Q', 82 =&gt; 'R', 83 =&gt; 'S', 84 =&gt; 'T', 85 =&gt; 'U', 86 =&gt; 'V', 87 =&gt; 'W', 88 =&gt; 'X', 89 =&gt; 'Y', 90 =&gt; 'Z', 91 =&gt; '[', 92 =&gt; '\\', 93 =&gt; ']', 94 =&gt; '^', 95 =&gt; '_', 96 =&gt; '`', 97 =&gt; 'a', 98 =&gt; 'b', 99 =&gt; 'c', 100 =&gt; 'd', 101 =&gt; 'e', 102 =&gt; 'f', 103 =&gt; 'g', 104 =&gt; 'h', 105 =&gt; 'i', 106 =&gt; 'j', 107 =&gt; 'k', 108 =&gt; 'l', 109 =&gt; 'm', 110 =&gt; 'n', 111 =&gt; 'o', 112 =&gt; 'p', 113 =&gt; 'q', 114 =&gt; 'r', 115 =&gt; 's', 116 =&gt; 't', 117 =&gt; 'u', 118 =&gt; 'v', 119 =&gt; 'w', 120 =&gt; 'x', 121 =&gt; 'y', 122 =&gt; 'z', 123 =&gt; '{', 124 =&gt; '|', 125 =&gt; '}' ); public static function fromNumber($number) { $string = ''; while($number) { $value = substr($number, 0, 2); $number = substr($number, 2); if($value &lt; 32) { $value .= substr($number, 0, 1); $number = substr($number, 1); } $string .= self::$charset[ (int) $value]; } return $string; } public static function fromString($string) { $number = ''; foreach(str_split($string) as $char) $number .= ord($char); return $number; } } $string = 'this is my test string to convert'; $number = intnum::fromString($string); $string = intnum::fromNumber($number); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload