Uređivanje

Dijeli putem


Encoding Class

Definition

Represents a character encoding.

public ref class Encoding abstract
public ref class Encoding abstract : ICloneable
public abstract class Encoding
public abstract class Encoding : ICloneable
[System.Serializable]
public abstract class Encoding
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Encoding : ICloneable
type Encoding = class
type Encoding = class
    interface ICloneable
[<System.Serializable>]
type Encoding = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Encoding = class
    interface ICloneable
Public MustInherit Class Encoding
Public MustInherit Class Encoding
Implements ICloneable
Inheritance
Encoding
Derived
Attributes
Implements

Examples

The following example converts a string from one encoding to another.

Note

The byte[] array is the only type in this example that contains the encoded data. The .NET Char and String types are themselves Unicode, so the GetChars call decodes the data back to Unicode.

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 array.
   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.
   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 );
}
// 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 (?)
using System;
using System.Text;

class Example
{
   static void 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 array.
      byte[] unicodeBytes = unicode.GetBytes(unicodeString);

      // Perform the conversion from one encoding to the other.
      byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
         
      // Convert the new byte[] into a char[] and then into a string.
      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);
   }
}
// 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 (?)
Imports System.Text

Class Example
   Shared Sub Main()
      Dim unicodeString As String = "This string contains the unicode character Pi (" & ChrW(&H03A0) & ")"

      ' Create two different encodings.
      Dim ascii As Encoding = Encoding.ASCII
      Dim unicode As Encoding = Encoding.Unicode

      ' Convert the string into a byte array.
      Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString)

      ' Perform the conversion from one encoding to the other.
      Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)

      ' Convert the new byte array into a char array and then into a string.
      Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
      ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
      Dim asciiString As 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)
   End Sub
End Class
' 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 (?)

Remarks

For more information about this API, see Supplemental API remarks for Encoding.

Constructors

Encoding()

Initializes a new instance of the Encoding class.

Encoding(Int32, EncoderFallback, DecoderFallback)

Initializes a new instance of the Encoding class that corresponds to the specified code page with the specified encoder and decoder fallback strategies.

Encoding(Int32)

Initializes a new instance of the Encoding class that corresponds to the specified code page.

Properties

ASCII

Gets an encoding for the ASCII (7-bit) character set.

BigEndianUnicode

Gets an encoding for the UTF-16 format that uses the big endian byte order.

BodyName

When overridden in a derived class, gets a name for the current encoding that can be used with mail agent body tags.

CodePage

When overridden in a derived class, gets the code page identifier of the current Encoding.

DecoderFallback

Gets or sets the DecoderFallback object for the current Encoding object.

Default

Gets the default encoding for this .NET implementation.

EncoderFallback

Gets or sets the EncoderFallback object for the current Encoding object.

EncodingName

When overridden in a derived class, gets the human-readable description of the current encoding.

HeaderName

When overridden in a derived class, gets a name for the current encoding that can be used with mail agent header tags.

IsBrowserDisplay

When overridden in a derived class, gets a value indicating whether the current encoding can be used by browser clients for displaying content.

IsBrowserSave

When overridden in a derived class, gets a value indicating whether the current encoding can be used by browser clients for saving content.

IsMailNewsDisplay

When overridden in a derived class, gets a value indicating whether the current encoding can be used by mail and news clients for displaying content.

IsMailNewsSave

When overridden in a derived class, gets a value indicating whether the current encoding can be used by mail and news clients for saving content.

IsReadOnly

When overridden in a derived class, gets a value indicating whether the current encoding is read-only.

IsSingleByte

When overridden in a derived class, gets a value indicating whether the current encoding uses single-byte code points.

Latin1

Gets an encoding for the Latin1 character set (ISO-8859-1).

Preamble

When overridden in a derived class, returns a span containing the sequence of bytes that specifies the encoding used.

Unicode

Gets an encoding for the UTF-16 format using the little endian byte order.

UTF32

Gets an encoding for the UTF-32 format using the little endian byte order.

UTF7
Obsolete.

Gets an encoding for the UTF-7 format.

UTF8

Gets an encoding for the UTF-8 format.

WebName

When overridden in a derived class, gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.

WindowsCodePage

When overridden in a derived class, gets the Windows operating system code page that most closely corresponds to the current encoding.

Methods

Clone()

When overridden in a derived class, creates a shallow copy of the current Encoding object.

Convert(Encoding, Encoding, Byte[], Int32, Int32)

Converts a range of bytes in a byte array from one encoding to another.

Convert(Encoding, Encoding, Byte[])

Converts an entire byte array from one encoding to another.

CreateTranscodingStream(Stream, Encoding, Encoding, Boolean)

Creates a Stream that serves to transcode data between an inner Encoding and an outer Encoding, similar to Convert(Encoding, Encoding, Byte[]).

Equals(Object)

Determines whether the specified Object is equal to the current instance.

GetByteCount(Char[], Int32, Int32)

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array.

GetByteCount(Char[])

When overridden in a derived class, calculates the number of bytes produced by encoding all the characters in the specified character array.

GetByteCount(Char*, Int32)

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

GetByteCount(ReadOnlySpan<Char>)

When overridden in a derived class, calculates the number of bytes produced by encoding the characters in the specified character span.

GetByteCount(String, Int32, Int32)

When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified string.

GetByteCount(String)

When overridden in a derived class, calculates the number of bytes produced by encoding the characters in the specified string.

GetBytes(Char[], Int32, Int32, Byte[], Int32)

When overridden in a derived class, encodes a set of characters from the specified character array into the specified byte array.

GetBytes(Char[], Int32, Int32)

When overridden in a derived class, encodes a set of characters from the specified character array into a sequence of bytes.

GetBytes(Char[])

When overridden in a derived class, encodes all the characters in the specified character array into a sequence of bytes.

GetBytes(Char*, Int32, Byte*, Int32)

When overridden in a derived class, encodes a set of characters starting at the specified character pointer into a sequence of bytes that are stored starting at the specified byte pointer.

GetBytes(ReadOnlySpan<Char>, Span<Byte>)

When overridden in a derived class, encodes into a span of bytes a set of characters from the specified read-only span.

GetBytes(String, Int32, Int32, Byte[], Int32)

When overridden in a derived class, encodes a set of characters from the specified string into the specified byte array.

GetBytes(String, Int32, Int32)

When overridden in a derived class, encodes into an array of bytes the number of characters specified by count in the specified string, starting from the specified index.

GetBytes(String)

When overridden in a derived class, encodes all the characters in the specified string into a sequence of bytes.

GetCharCount(Byte[], Int32, Int32)

When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

GetCharCount(Byte[])

When overridden in a derived class, calculates the number of characters produced by decoding all the bytes in the specified byte array.

GetCharCount(Byte*, Int32)

When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer.

GetCharCount(ReadOnlySpan<Byte>)

When overridden in a derived class, calculates the number of characters produced by decoding the provided read-only byte span.

GetChars(Byte[], Int32, Int32, Char[], Int32)

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array.

GetChars(Byte[], Int32, Int32)

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a set of characters.

GetChars(Byte[])

When overridden in a derived class, decodes all the bytes in the specified byte array into a set of characters.

GetChars(Byte*, Int32, Char*, Int32)

When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are stored starting at the specified character pointer.

GetChars(ReadOnlySpan<Byte>, Span<Char>)

When overridden in a derived class, decodes all the bytes in the specified read-only byte span into a character span.

GetDecoder()

When overridden in a derived class, obtains a decoder that converts an encoded sequence of bytes into a sequence of characters.

GetEncoder()

When overridden in a derived class, obtains an encoder that converts a sequence of Unicode characters into an encoded sequence of bytes.

GetEncoding(Int32, EncoderFallback, DecoderFallback)

Returns the encoding associated with the specified code page identifier. Parameters specify an error handler for characters that cannot be encoded and byte sequences that cannot be decoded.

GetEncoding(Int32)

Returns the encoding associated with the specified code page identifier.

GetEncoding(String, EncoderFallback, DecoderFallback)

Returns the encoding associated with the specified code page name. Parameters specify an error handler for characters that cannot be encoded and byte sequences that cannot be decoded.

GetEncoding(String)

Returns the encoding associated with the specified code page name.

GetEncodings()

Returns an array that contains all encodings.

GetHashCode()

Returns the hash code for the current instance.

GetMaxByteCount(Int32)

When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters.

GetMaxCharCount(Int32)

When overridden in a derived class, calculates the maximum number of characters produced by decoding the specified number of bytes.

GetPreamble()

When overridden in a derived class, returns a sequence of bytes that specifies the encoding used.

GetString(Byte[], Int32, Int32)

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a string.

GetString(Byte[])

When overridden in a derived class, decodes all the bytes in the specified byte array into a string.

GetString(Byte*, Int32)

When overridden in a derived class, decodes a specified number of bytes starting at a specified address into a string.

GetString(ReadOnlySpan<Byte>)

When overridden in a derived class, decodes all the bytes in the specified byte span into a string.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsAlwaysNormalized()

Gets a value indicating whether the current encoding is always normalized, using the default normalization form.

IsAlwaysNormalized(NormalizationForm)

When overridden in a derived class, gets a value indicating whether the current encoding is always normalized, using the specified normalization form.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
RegisterProvider(EncodingProvider)

Registers an encoding provider.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
TryGetBytes(ReadOnlySpan<Char>, Span<Byte>, Int32)

Encodes into a span of bytes a set of characters from the specified read-only span if the destination is large enough.

TryGetChars(ReadOnlySpan<Byte>, Span<Char>, Int32)

Decodes into a span of chars a set of bytes from the specified read-only span if the destination is large enough.

Extension Methods

GetBytes(Encoding, ReadOnlySequence<Char>, IBufferWriter<Byte>)

Decodes the specified ReadOnlySequence<T> to bytes using the specified Encoding and writes the result to writer.

GetBytes(Encoding, ReadOnlySequence<Char>, Span<Byte>)

Encodes the specified ReadOnlySequence<T> to bytes using the specified Encoding and outputs the result to bytes.

GetBytes(Encoding, ReadOnlySequence<Char>)

Encodes the specified ReadOnlySequence<T> into a Byte array using the specified Encoding.

GetBytes(Encoding, ReadOnlySpan<Char>, IBufferWriter<Byte>)

Encodes the specified ReadOnlySpan<T> to bytes using the specified Encoding and writes the result to writer.

GetChars(Encoding, ReadOnlySequence<Byte>, IBufferWriter<Char>)

Decodes the specified ReadOnlySequence<T> to chars using the specified Encoding and writes the result to writer.

GetChars(Encoding, ReadOnlySequence<Byte>, Span<Char>)

Decodes the specified ReadOnlySequence<T> to chars using the specified Encoding and outputs the result to chars.

GetChars(Encoding, ReadOnlySpan<Byte>, IBufferWriter<Char>)

Decodes the specified ReadOnlySpan<T> to chars using the specified Encoding and writes the result to writer.

GetString(Encoding, ReadOnlySequence<Byte>)

Decodes the specified ReadOnlySequence<T> into a String using the specified Encoding.

Applies to

See also