Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why have you converted binary (image) data to a string? This makes no sense... unless you are using base-64?</p> <p>Anyway, to reverse what you have done, you could try using <code>new MemoryStream(Encoding.UTF8.GetBytes(text))</code>?</p> <p>This will create a new MemoryStream primed with the string (via UTF8). Personally, I doubt it will work - you are going to run into a lot of encoding issues treating raw binary as UTF8 data... I expect either the read or write (or both) to throw an exception.</p> <p>(edit)</p> <p>I should add that to work with base-64, simply get the data as a <code>byte[]</code>, then call <code>Convert.ToBase64String(...)</code>; and to get back the array, just use <code>Convert.FromBase64String(...)</code>.</p> <hr> <p>Re your edit, this is precisely what I tried to warn about above... in .NET, a string is not just a <code>byte[]</code>, so you can't simply fill it with binary image data. A lot of the data simply won't make sense to the encoding, so might be quietly dropped (or an exception thrown).</p> <p>To handle raw binary (such as images) as strings, you need to use base-64 encoding; this adds size, however. Note that <code>WebClient</code> might make this simpler, as it exposes <code>byte[]</code> functionality directly:</p> <pre><code>using(WebClient wc = new WebClient()) { byte[] raw = wc.DownloadData("http://www.google.com/images/nav_logo.png") //... } </code></pre> <p>Anyway, using a standard <code>Stream</code> approach, here's how to encode and decode the base-64:</p> <pre><code> // ENCODE // where "s" is our original stream string base64; // first I need the data as a byte[]; I'll use // MemoryStream, as a convenience; if you already // have the byte[] you can skip this using (MemoryStream ms = new MemoryStream()) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = s.Read(buffer, 0, buffer.Length)) &gt; 0) { ms.Write(buffer, 0, bytesRead); } base64 = Convert.ToBase64String(ms.GetBuffer(), 0, (int) ms.Length); } // DECODE byte[] raw = Convert.FromBase64String(base64); using (MemoryStream decoded = new MemoryStream(raw)) { // "decoded" now primed with the binary } </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