Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What is the range of your numbers? Assuming they can fit in a 16-bit integer, I would:</p> <ul> <li>Store all your numbers as <a href="http://en.wikipedia.org/wiki/Short_integer" rel="nofollow noreferrer">16-bit integers</a> (2 bytes per number, range -32,768 to 32,767)</li> <li>Build a bytestream of 16-bit integers (<a href="http://en.wikipedia.org/wiki/External_Data_Representation" rel="nofollow noreferrer">XDR</a> might be a good option here; at very least, make sure to handle <a href="http://en.wikipedia.org/wiki/Endianness" rel="nofollow noreferrer">endianness</a> correctly)</li> <li><a href="http://en.wikipedia.org/wiki/Base64" rel="nofollow noreferrer">Base64</a> encode the bytestream, using the modified base64 encoding for URLs (net is about 3 characters per number)</li> </ul> <p>As an added bonus you don't need comma characters anymore because you know each number is 2 bytes.</p> <p>Alternatively, if that isn't good enough, I'd use <a href="http://zlib.net/" rel="nofollow noreferrer">zlib</a> to compress your stream of integers and then base64 the zlib-compressed stream. You can also switch to 32-bit integers if 16-bit isn't a large enough range (i.e. if you really need numbers in the 1,000,000,000 range).</p> <p><strong>Edit:</strong></p> <p>Maybe too late, but here's an implementation that might do what you need:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Scratch { class Program { static void Main(string[] args) { //var ids = new[] { 1000000012, 1000000021, 1000000013, 1000000022 }; var rand = new Random(); var ids = new int[rand.Next(20)]; for(var i = 0; i &lt; ids.Length; i++) { ids[i] = rand.Next(); } WriteIds(ids); var s = IdsToString(ids); Console.WriteLine("\nResult string is: {0}", s); var newIds = StringToIds(s); WriteIds(newIds); Console.ReadLine(); } public static void WriteIds(ICollection&lt;Int32&gt; ids) { Console.Write("\nIDs: "); bool comma = false; foreach(var id in ids) { if(comma) { Console.Write(","); } else { comma = true; } Console.Write(id); } Console.WriteLine(); } public static string IdsToString(ICollection&lt;Int32&gt; ids) { var allbytes = new List&lt;byte&gt;(); foreach(var id in ids) { var bytes = BitConverter.GetBytes(id); allbytes.AddRange(bytes); } var str = Convert.ToBase64String(allbytes.ToArray(), Base64FormattingOptions.None); return str.Replace('+', '-').Replace('/', '_').Replace('=', '.'); } public static ICollection&lt;Int32&gt; StringToIds(string idstring) { var result = new List&lt;Int32&gt;(); var str = idstring.Replace('-', '+').Replace('_', '/').Replace('.', '='); var bytes = Convert.FromBase64String(str); for(var i = 0; i &lt; bytes.Length; i += 4) { var id = BitConverter.ToInt32(bytes, i); result.Add(id); } return result; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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