Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two clarifications on the previous answers:</p> <p>There is no <code>Encoding.GetText</code> method (unless it was introduced specifically for the WP7 framework). The method should presumably be <code>Encoding.GetString</code>.</p> <p>The <code>Encoding.GetString</code> method takes a <code>byte[]</code> parameter, not a <code>string</code>. All strings in .NET are internally represented as UTF-16; there is no way of having a “string in ISO-8859-1 format”. Thus, you must be careful how you <em>read</em> your source (file, network), rather than how you process your string. </p> <p>For example, to read from a text file encoded in ISO-8859-1, you could use:</p> <pre><code>string text = File.ReadAllText(path, Encoding.GetEncoding("iso-8859-1")); </code></pre> <p>To save to a text file encoded in UTF-8, you could use:</p> <pre><code>File.WriteAllText(path, text, Encoding.UTF8); </code></pre> <p><strong>Reply</strong> to comment:</p> <p>Yes. You can use <code>Encoding.GetString</code> to decode your byte array (assuming it contains character values for text under a particular encoding) into a <code>string</code>, and <code>Encoding.GetBytes</code> to convert your <code>string</code> back into a byte array (possibly of a different encoding), as demonstrated in the other answers. </p> <p>The concept of “encoding” relates to how byte sequences (be they a <code>byte[]</code> array in memory or the content of a file on disk) are to be interpreted. The <code>string</code> class is oblivious to the encoding that the text was read from, or should be saved to.</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