Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another way of doing this might be to encode to binary types such as signed/unsigned ints, and manually decode as at <a href="http://snippets.dzone.com/posts/show/685" rel="nofollow noreferrer">http://snippets.dzone.com/posts/show/685</a> which would require server side code to create the binary data.</p> <p>You could then huffman compression or something similar like RLE (see <a href="http://rosettacode.org/wiki/Run-length_encoding#JavaScript" rel="nofollow noreferrer">http://rosettacode.org/wiki/Run-length_encoding#JavaScript</a> for an implementation, though it may have some issues in IE without modifying) to compress the data further.</p> <p><strong>EDIT</strong>: Alternatively, you could convert the numbers themselves to a base (radix) in the unencoded URI character range (see <a href="http://en.wikipedia.org/wiki/Percent-encoding" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Percent-encoding</a>) which should work well if many of the numbers are larger than 2 digits. I converted the code at <a href="http://code.activestate.com/recipes/111286-numeric-base-converter-that-accepts-arbitrary-digi/" rel="nofollow noreferrer">http://code.activestate.com/recipes/111286-numeric-base-converter-that-accepts-arbitrary-digi/</a> from python to do this.</p> <p>It currently doesn't handle floats, but it could be done fairly easily:</p> <pre><code>function get_map(s) { d = {} for (var i=0; i&lt;s.length; i++) { d[s.charAt(i)] = i} d.length = s.length d._s = s return d} var separate_with = '~'; var encodable = get_map('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.'); // - is reserved for negatives obviously :-P var base10 = get_map('0123456789') // UNCOMMENT ME for length/speed testing in a wider base! // You may wish to experiment with the ranges for a happy medium between bandwidth and DB space :-P /*var encodable = '' for (var i=1; i&lt;128; i++) { encodable += String.fromCharCode(i) } encodable = get_map(encodable)*/ function baseconvert(number, fromdigits, todigits) { var number = String(number) if (number.charAt(0) == '-') { number = number.slice(1, number.length) neg=1} else { neg=0} // make an integer out of the number var x = 0 for (var i=0; i&lt;number.length; i++) { var digit = number.charAt(i) x = x*fromdigits.length + fromdigits[digit] } // create the result in base 'todigits.length' res = "" while (x&gt;0) { remainder = x % todigits.length res = todigits._s.charAt(remainder) + res x = parseInt(x/todigits.length) } if (neg) res = "-"+res return res } function encodeNums(L) { var r = [] for (var i=0; i&lt;L.length; i++) { r.push(baseconvert(L[i], base10, encodable)) } return r.join(separate_with) } function decodeNums(s) { var r = [] var s = s.split(separate_with) for (var i=0; i&lt;s.length; i++) { r.push(parseInt(baseconvert(s[i], encodable, base10))) } return r } var test = [5, 654645, 24324, 652124, 65, 65289543, 65278432, 643175874158, 652754327543] alert(encodeNums(test)) alert(decodeNums(encodeNums(test))) </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