Encoding.Convert Method (Encoding, Encoding, Byte[])
Assembly: mscorlib (in mscorlib.dll)
public: static array<unsigned char>^ Convert ( Encoding^ srcEncoding, Encoding^ dstEncoding, array<unsigned char>^ bytes )
public static byte[] Convert ( Encoding srcEncoding, Encoding dstEncoding, byte[] bytes )
public static function Convert ( srcEncoding : Encoding, dstEncoding : Encoding, bytes : byte[] ) : byte[]
Not applicable.
Parameters
- srcEncoding
The encoding format of bytes.
- dstEncoding
The target encoding format.
- bytes
Return Value
An array of type Byte containing the results of converting bytes from srcEncoding to dstEncoding.| Exception type | Condition |
|---|---|
| srcEncoding is a null reference (Nothing in Visual Basic). -or- dstEncoding is a null reference (Nothing in Visual Basic). -or- bytes is a null reference (Nothing in Visual Basic). | |
| A fallback occurred (see Understanding Encodings for complete explanation) -and- srcEncoding. DecoderFallback is set to DecoderExceptionFallback. | |
| A fallback occurred (see Understanding Encodings for complete explanation) -and- dstEncoding. EncoderFallback is set to EncoderExceptionFallback. |
The following code example converts a string from one encoding to another.
using namespace System; using namespace System::Text; int main() { String^ unicodeString = "This string contains the unicode character Pi(\u03a0)"; // Create two different encodings. Encoding^ ascii = Encoding::ASCII; Encoding^ unicode = Encoding::Unicode; // Convert the string into a Byte->Item[]. array<Byte>^unicodeBytes = unicode->GetBytes( unicodeString ); // Perform the conversion from one encoding to the other. array<Byte>^asciiBytes = Encoding::Convert( unicode, ascii, unicodeBytes ); // Convert the new Byte into[] a char and[] then into a string. // This is a slightly different approach to converting to illustrate // the use of GetCharCount/GetChars. array<Char>^asciiChars = gcnew array<Char>(ascii->GetCharCount( asciiBytes, 0, asciiBytes->Length )); ascii->GetChars( asciiBytes, 0, asciiBytes->Length, asciiChars, 0 ); String^ asciiString = gcnew String( asciiChars ); // Display the strings created before and after the conversion. Console::WriteLine( "Original String*: {0}", unicodeString ); Console::WriteLine( "Ascii converted String*: {0}", asciiString ); }
package ConvertExample;
import System.*;
import System.Text.*;
class ConvertExampleClass
{
public static void main(String[] args)
{
String unicodeString =
"This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.get_ASCII();
Encoding unicode = Encoding.get_Unicode();
// Convert the string into a byte[].
ubyte unicodeBytes[] = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
ubyte asciiBytes[] = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char asciiChars[] = new
char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.length, asciiChars, 0);
String asciiString = new String(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
} //main
} //ConvertExampleClass
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.