Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code is bizarre to start with:</p> <pre><code>byte[] byteArray = Encoding.UTF8.GetBytes(value); int indexBA = 0; foreach (char item in value.ToCharArray()) { byteArray[indexBA++] = (byte)item; } </code></pre> <p>You're converting it to a byte array using UTF-8 encoding... and <em>then</em> you're overwriting the contents of that array (or at least <em>some</em> of the contents of that array) by casting each character to a byte - which is effectively applying ISO-Latin-1 encoding.</p> <p>Then later on, you're converting <em>arbitrary binary data</em> to a string like this:</p> <pre><code>byteArray = ms.ToArray(); System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length); foreach (byte item in byteArray) { sB.Append((char)item); } </code></pre> <p><strong>Don't do that.</strong> It's opaque binary data - the chances of the "string" you're creating (again, effectively created via ISO-8859-1) being properly transferable are very slim.</p> <p>When encoding arbitrary binary data as a string, you should almost always use Base64:</p> <pre><code>string base64 = Convert.ToBase64String(byteArray); </code></pre> <p>You're then <em>also</em> using the data as URL-encoded form data - despite the fact that the string could quite easily include characters such as <code>&amp;</code> and <code>%</code> which have special meaning within URL encoded text. Don't do that either.</p> <p>Basically, you should:</p> <ul> <li>Choose which encoding you want to use for the initial text to binary conversion. UTF-8 is a good choice here, as it can represent all of Unicode.</li> <li>Perform the compression (and no, flushing should <em>not</em> cause problems here, although you should also close anyway - ideally via a <code>using</code> statement)</li> <li>Transform the binary data back into text (assuming you really have to) using base64. If you're going to use this as a URL parameter, you should use a web-safe variant of base64, as described on the <a href="http://en.wikipedia.org/wiki/Base64" rel="nofollow">Wikipedia base64 page</a>.</li> </ul> <p>To decompress, you'll obviously need to perform the base64 to binary conversion before decompression.</p> <p>If possible, it would be significantly more efficient (in terms of the transferred data) to post the compressed data <em>as binary data</em> rather than as a URL-encoded form parameter.</p>
 

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