Condividi tramite


UTF7Encoding.GetBytes Metodo

Definizione

Codifica un set di caratteri in una sequenza di byte.

Overload

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

Codifica un set di caratteri a partire dal puntatore ai caratteri specificato in una sequenza di byte archiviati a partire dal puntatore ai byte specificato.

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

Codifica un set di caratteri dalla matrice di caratteri specificata nella matrice di byte specificata.

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

Codifica un set di caratteri dall'oggetto String specificato nella matrice di byte specificata.

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

Importante

Questa API non è conforme a CLS.

Codifica un set di caratteri a partire dal puntatore ai caratteri specificato in una sequenza di byte archiviati a partire dal puntatore ai byte specificato.

public:
 override int GetBytes(char* chars, int charCount, System::Byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int

Parametri

chars
Char*

Puntatore al primo carattere da codificare.

charCount
Int32

Numero di caratteri da codificare.

bytes
Byte*

Puntatore alla posizione in cui iniziare a scrivere la sequenza di byte risultante.

byteCount
Int32

Numero massimo di byte da scrivere.

Restituisce

Int32

Numero effettivo di byte scritti nella posizione indicata da bytes.

Attributi

Eccezioni

chars è null (Nothing).

-oppure- bytes è null (Nothing).

charCount o byteCount è minore di zero.

byteCount è minore del numero di byte risultante.

Si è verificato un fallback. Per una spiegazione più completa, vedere Codifica dei caratteri in .NET Framework.

-e- EncoderFallback è impostato su EncoderExceptionFallback.

Commenti

Per calcolare la dimensione esatta della matrice richiesta da GetBytes per archiviare i byte risultanti, l'applicazione usa GetByteCount. Per calcolare le dimensioni massime della matrice, l'applicazione deve usare GetMaxByteCount. Il GetByteCount metodo in genere consente l'allocazione di una quantità di memoria inferiore, mentre il GetMaxByteCount metodo viene in genere eseguito più velocemente.

I dati da convertire, ad esempio i dati letti da un flusso, potrebbero essere disponibili solo in blocchi sequenziali. In questo caso, o se la quantità di dati è così grande che deve essere divisa in blocchi più piccoli, l'applicazione deve usare rispettivamente l'oggetto Decoder o Encoder fornito dal GetDecoder metodo o dal GetEncoder metodo .

Nota

UTF7Encoding non fornisce il rilevamento degli errori. I caratteri non validi vengono codificati come carattere di base 64 modificato. Per motivi di sicurezza, è consigliabile usare UTF8Encoding, UnicodeEncodingo UTF32Encoding e abilitare il rilevamento degli errori.

Vedi anche

Si applica a

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

Codifica un set di caratteri dalla matrice di caratteri specificata nella matrice di byte specificata.

public:
 override int GetBytes(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);
override this.GetBytes : char[] * int * int * byte[] * int -> int
Public Overrides Function GetBytes (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer

Parametri

chars
Char[]

Matrice di caratteri contenente il set di caratteri da codificare.

charIndex
Int32

Indice del primo carattere da codificare.

charCount
Int32

Numero di caratteri da codificare.

bytes
Byte[]

Matrice di byte che deve contenere la sequenza di byte risultante.

byteIndex
Int32

Indice in corrispondenza del quale iniziare a scrivere la sequenza di byte risultante.

Restituisce

Int32

Numero effettivo di byte scritti in bytes.

Eccezioni

chars è null (Nothing).

-oppure- bytes è null (Nothing).

charIndex o charCount o byteIndex è minore di zero.

-oppure- charIndex e charCount non identificano un intervallo valido in chars.

-oppure- byteIndex non è un indice valido in bytes.

bytes non dispone di sufficiente capacità da byteIndex alla fine della matrice per contenere i byte risultanti.

Si è verificato un fallback. Per una spiegazione più completa, vedere Codifica dei caratteri in .NET Framework.

-e- EncoderFallback è impostato su EncoderExceptionFallback.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare il GetBytes metodo per codificare un intervallo di caratteri da un String oggetto e archiviare i byte codificati in un intervallo di elementi in una matrice di byte.

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Byte>^bytes;
   
   // Unicode characters.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   int byteCount = utf7->GetByteCount( chars, 1, 2 );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf7->GetBytes( chars, 1, 2, bytes, 0 );
   Console::WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount );
   Console::Write( "Encoded bytes: " );
   IEnumerator^ myEnum = bytes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Byte b = safe_cast<Byte>(myEnum->Current);
      Console::Write( "[{0}]", b );
   }

   Console::WriteLine();
}
using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        Byte[] bytes;
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };
        
        UTF7Encoding utf7 = new UTF7Encoding();
        
        int byteCount = utf7.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf7.GetBytes(chars, 1, 2, bytes, 0);
        
        Console.WriteLine(
            "{0} bytes used to encode characters.", bytesEncodedCount
        );

        Console.Write("Encoded bytes: ");
        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.strings

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        
        Dim utf7 As New UTF7Encoding()
        
        Dim byteCount As Integer = utf7.GetByteCount(chars, 1, 2)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf7.GetBytes(chars, 1, 2, bytes, 0)
        
        Console.WriteLine("{0} bytes used to encode characters.", bytesEncodedCount)
        
        Console.Write("Encoded bytes: ")
        Dim b As Byte
        For Each b In  bytes
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine()
    End Sub
End Class

Commenti

Per calcolare la dimensione esatta della matrice richiesta da GetBytes per archiviare i byte risultanti, l'applicazione usa GetByteCount. Per calcolare le dimensioni massime della matrice, l'applicazione deve usare GetMaxByteCount. Il GetByteCount metodo in genere consente l'allocazione di una quantità di memoria inferiore, mentre il GetMaxByteCount metodo viene in genere eseguito più velocemente.

I dati da convertire, ad esempio i dati letti da un flusso, potrebbero essere disponibili solo in blocchi sequenziali. In questo caso, o se la quantità di dati è così grande che deve essere divisa in blocchi più piccoli, l'applicazione deve usare rispettivamente l'oggetto Decoder o Encoder fornito dal GetDecoder metodo o dal GetEncoder metodo .

Nota

UTF7Encoding non fornisce il rilevamento degli errori. I caratteri non validi vengono codificati come carattere di base 64 modificato. Per motivi di sicurezza, è consigliabile usare UTF8Encoding, UnicodeEncodingo UTF32Encoding e abilitare il rilevamento degli errori.

Vedi anche

Si applica a

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

Codifica un set di caratteri dall'oggetto String specificato nella matrice di byte specificata.

public:
 override int GetBytes(System::String ^ s, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public override int GetBytes (string s, int charIndex, int charCount, byte[] bytes, int byteIndex);
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes (string s, int charIndex, int charCount, byte[] bytes, int byteIndex);
override this.GetBytes : string * int * int * byte[] * int -> int
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetBytes : string * int * int * byte[] * int -> int
Public Overrides Function GetBytes (s As String, charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer

Parametri

s
String

Oggetto String contenente il set di caratteri da codificare.

charIndex
Int32

Indice del primo carattere da codificare.

charCount
Int32

Numero di caratteri da codificare.

bytes
Byte[]

Matrice di byte che deve contenere la sequenza di byte risultante.

byteIndex
Int32

Indice in corrispondenza del quale iniziare a scrivere la sequenza di byte risultante.

Restituisce

Int32

Numero effettivo di byte scritti in bytes.

Attributi

Eccezioni

s è null (Nothing).

-oppure- bytes è null (Nothing).

charIndex o charCount o byteIndex è minore di zero.

-oppure- charIndex e charCount non identificano un intervallo valido in chars.

-oppure- byteIndex non è un indice valido in bytes.

bytes non dispone di sufficiente capacità da byteIndex alla fine della matrice per contenere i byte risultanti.

Si è verificato un fallback. Per una spiegazione più completa, vedere Codifica dei caratteri in .NET Framework.

-e- EncoderFallback è impostato su EncoderExceptionFallback.

Esempio

Nell'esempio di codice seguente viene illustrato come usare il GetBytes metodo per codificare un intervallo di elementi da una matrice di caratteri Unicode e archiviare i byte codificati in un intervallo di elementi in una matrice di byte.

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Byte>^bytes;
   
   // Unicode characters.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   int byteCount = utf7->GetByteCount( chars, 1, 2 );
   bytes = gcnew array<Byte>(byteCount);
   int bytesEncodedCount = utf7->GetBytes( chars, 1, 2, bytes, 0 );
   Console::WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount );
   Console::Write( "Encoded bytes: " );
   IEnumerator^ myEnum = bytes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Byte b = safe_cast<Byte>(myEnum->Current);
      Console::Write( "[{0}]", b );
   }

   Console::WriteLine();
}
using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        Byte[] bytes;
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };
        
        UTF7Encoding utf7 = new UTF7Encoding();
        
        int byteCount = utf7.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf7.GetBytes(chars, 1, 2, bytes, 0);
        
        Console.WriteLine(
            "{0} bytes used to encode characters.", bytesEncodedCount
        );

        Console.Write("Encoded bytes: ");
        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.strings

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        
        Dim utf7 As New UTF7Encoding()
        
        Dim byteCount As Integer = utf7.GetByteCount(chars, 1, 2)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf7.GetBytes(chars, 1, 2, bytes, 0)
        
        Console.WriteLine("{0} bytes used to encode characters.", bytesEncodedCount)
        
        Console.Write("Encoded bytes: ")
        Dim b As Byte
        For Each b In  bytes
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine()
    End Sub
End Class

Commenti

Per calcolare la dimensione esatta della matrice richiesta da GetBytes per archiviare i byte risultanti, l'applicazione usa GetByteCount. Per calcolare le dimensioni massime della matrice, l'applicazione deve usare GetMaxByteCount. Il GetByteCount metodo in genere consente l'allocazione di una quantità di memoria inferiore, mentre il GetMaxByteCount metodo viene in genere eseguito più velocemente.

I dati da convertire, ad esempio i dati letti da un flusso, potrebbero essere disponibili solo in blocchi sequenziali. In questo caso, o se la quantità di dati è così grande che deve essere divisa in blocchi più piccoli, l'applicazione deve usare rispettivamente l'oggetto Decoder o Encoder fornito dal GetDecoder metodo o dal GetEncoder metodo .

Nota

UTF7Encoding non fornisce il rilevamento degli errori. I caratteri non validi vengono codificati come carattere di base 64 modificato. Per motivi di sicurezza, è consigliabile usare UTF8Encoding, UnicodeEncodingo UTF32Encoding e abilitare il rilevamento degli errori.

Vedi anche

Si applica a