Редагувати

Поділитися через


UnicodeEncoding.GetCharCount Method

Definition

Calculates the number of characters produced by decoding a sequence of bytes.

Overloads

GetCharCount(Byte*, Int32)

Calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer.

GetCharCount(Byte[], Int32, Int32)

Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

GetCharCount(Byte*, Int32)

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs

Important

This API is not CLS-compliant.

Calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer.

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

Parameters

bytes
Byte*

A pointer to the first byte to decode.

count
Int32

The number of bytes to decode.

Returns

The number of characters produced by decoding the specified sequence of bytes.

Attributes

Exceptions

bytes is null (Nothing).

count is less than zero.

-or-

The resulting number of bytes is greater than the maximum number that can be returned as an integer.

Error detection is enabled, and bytes contains an invalid sequence of bytes.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Remarks

To calculate the exact array size that GetChars requires to store the resulting characters, the application uses GetCharCount. To calculate the maximum array size, the application should use GetMaxCharCount. The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

With error detection, an invalid sequence causes this method to throw a ArgumentException. Without error detection, invalid sequences are ignored, and no exception is thrown.

See also

Applies to

GetCharCount(Byte[], Int32, Int32)

Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs
Source:
UnicodeEncoding.cs

Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

public:
 override int GetCharCount(cli::array <System::Byte> ^ bytes, int index, int count);
public override int GetCharCount (byte[] bytes, int index, int count);
override this.GetCharCount : byte[] * int * int -> int
Public Overrides Function GetCharCount (bytes As Byte(), index As Integer, count As Integer) As Integer

Parameters

bytes
Byte[]

The byte array containing the sequence of bytes to decode.

index
Int32

The index of the first byte to decode.

count
Int32

The number of bytes to decode.

Returns

The number of characters produced by decoding the specified sequence of bytes.

Exceptions

bytes is null (Nothing).

index or count is less than zero.

-or-

index and count do not denote a valid range in bytes.

-or-

The resulting number of bytes is greater than the maximum number that can be returned as an integer.

Error detection is enabled, and bytes contains an invalid sequence of bytes.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Examples

The following example demonstrates how to use the GetCharCount method to return the number of characters produced by decoding a range of elements in a byte array using UnicodeEncoding.

using namespace System;
using namespace System::Text;
int main()
{
   array<Byte>^bytes = {85,0,110,0,105,0,99,0,111,0,100,0,101,0};
   UnicodeEncoding^ Unicode = gcnew UnicodeEncoding;
   int charCount = Unicode->GetCharCount( bytes, 2, 8 );
   Console::WriteLine( "{0} characters needed to decode bytes.", charCount );
}
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
        };

        UnicodeEncoding Unicode = new UnicodeEncoding();
        int charCount = Unicode.GetCharCount(bytes, 2, 8);
        Console.WriteLine(
            "{0} characters needed to decode bytes.", charCount
        );
    }
}
Imports System.Text

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte = {85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0}
        
        Dim uni As New UnicodeEncoding()
        Dim charCount As Integer = uni.GetCharCount(bytes, 2, 8)
        Console.WriteLine("{0} characters needed to decode bytes.", charCount)
    End Sub
End Class

Remarks

To calculate the exact array size required by GetChars to store the resulting characters, the application uses GetCharCount. To calculate the maximum array size, the application should use GetMaxCharCount. The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

With error detection, an invalid sequence causes this method to throw a ArgumentException. Without error detection, invalid sequences are ignored, and no exception is thrown.

See also

Applies to