convert binary to string not works
<p>i create a simple programm.</p>

<p>i create a string and compress it by following methods and store it in a binary data field type in sql server 2008 (binary(1000) field type).</p>

<p>when i read that binary data and result string is true like original string data with the same lenth and data but when i want to decompress it it gave me error.</p>

<p>i use this method to get bytes</p>

<pre><code> System.Text.ASCIIEncoding.ASCII.GetBytes(mystring)
</code></pre>

<p>and this method to get string</p>

<pre><code>System.Text.ASCIIEncoding.ASCII.GetString(binarydata)
</code></pre>

<p>in hard code in vs2012 editor result string works fine,but when i read it from sql give me this error in first line of decompression method:</p>

<pre><code> The input is not a valid Base-64 string as it contains a
 non-base 64 character, more   than two padding characters, 
 or a non-white space character among the padding characters.
</code></pre>

<p>whats wrongs with my codes?these two strings are same but </p>

<pre><code>string test1=Decompress("mystring"); this method works fine 
</code></pre>

<p>but this gave me that error and can not decompress retrieved string</p>

<pre><code>string temp=System.Text.ASCIIEncoding.ASCII.GetString(get data from sql) ;
string test2=Decompress(temp);
</code></pre>

<p>the comparing these string do not shows any deference</p>

<pre><code>int result = string.Compare(test1, test2); // result=0
</code></pre>

<p>my compression method;</p>

<pre><code>   public static string Compress(string text)
   {
       byte[] buffer = Encoding.UTF8.GetBytes(text);
       var memoryStream = new MemoryStream();
       using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
       {
           gZipStream.Write(buffer, 0, buffer.Length);
       }

       memoryStream.Position = 0;

       var compressedData = new byte[memoryStream.Length];
       memoryStream.Read(compressedData, 0, compressedData.Length);

       var gZipBuffer = new byte[compressedData.Length + 4];
       Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
       Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
       return Convert.ToBase64String(gZipBuffer);
   }
</code></pre>

<p>my decompression method;</p>

<pre><code>   public static string Decompress(string compressedText)
   {
       byte[] gZipBuffer = Convert.FromBase64String(compressedText);
       using (var memoryStream = new MemoryStream())
       {
           int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
           memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);

           var buffer = new byte[dataLength];

           memoryStream.Position = 0;
           using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
           {
               gZipStream.Read(buffer, 0, buffer.Length);
           }

           return Encoding.UTF8.GetString(buffer);
       }
   }
</code></pre>
