Encoding.Convert Method (Encoding, Encoding, Byte[])
Silverlight
Converts an entire byte array from one encoding to another.
Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Parameters
- srcEncoding
- Type: System.Text.Encoding
The encoding format of bytes.
- dstEncoding
- Type: System.Text.Encoding
The target encoding format.
- bytes
- Type: System.Byte[]
The bytes to convert.
Return Value
Type: System.Byte[]A byte array that contains the results of converting bytes from srcEncoding to dstEncoding.
| Exception | Condition |
|---|---|
| ArgumentNullException | srcEncoding is null. -or- dstEncoding is null. -or- bytes is null. |
| DecoderFallbackException | A fallback occurred (see Understanding Encodings for complete explanation). |
| EncoderFallbackException | A fallback occurred (see Understanding Encodings for complete explanation). |
The following code example converts a string from one encoding to another.
using System; using System.Text; class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { string unicodeString = "This string contains the unicode character Pi (\u03a0)"; // Create two different encodings. Encoding utf8 = Encoding.UTF8; Encoding unicode = Encoding.Unicode; // Convert the string into a byte[]. byte[] unicodeBytes = unicode.GetBytes(unicodeString); // Perform the conversion from one encoding to the other. byte[] utf8Bytes = Encoding.Convert(unicode, utf8, unicodeBytes); // Convert the new byte[] into a char[] and then into a string. char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)]; utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0); string utf8String = new string(utf8Chars); // Display the strings created before and after the conversion. outputBlock.Text += String.Format("Original string: {0}", unicodeString) + "\n"; outputBlock.Text += String.Format("Ascii converted string: {0}", utf8String) + "\n"; } } // The example displays the following output: // Original string: This string contains the unicode character Pi (Π) // Ascii converted string: This string contains the unicode character Pi (?)
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.