ASCIIEncoding.GetByteCount
Method
Definition
Overloads
| GetByteCount(String) |
Calculates the number of bytes produced by encoding the characters in the specified String. |
| GetByteCount(Char*, Int32) |
Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. |
| GetByteCount(Char[], Int32, Int32) |
Calculates the number of bytes produced by encoding a set of characters from the specified character array. |
GetByteCount(String)
Calculates the number of bytes produced by encoding the characters in the specified String.
public override int GetByteCount (string chars);
The number of bytes produced by encoding the specified characters.
chars is null.
The resulting number of bytes is greater than the maximum number that can be returned as an integer.
A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)
-and-
EncoderFallback is set to EncoderExceptionFallback.
Examples
The following example demonstrates how to use the GetByteCount method to return the number of bytes required to encode a string using ASCIIEncoding.
using namespace System;
using namespace System::Text;
int main()
{
String^ chars = "ASCII Encoding Example";
ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
int byteCount = ascii->GetByteCount( chars );
Console::WriteLine( " {0} bytes needed to encode string.", byteCount );
}
using System;
using System.Text;
class ASCIIEncodingExample {
public static void Main() {
String chars = "ASCII Encoding Example";
ASCIIEncoding ascii = new ASCIIEncoding();
int byteCount = ascii.GetByteCount(chars);
Console.WriteLine(
"{0} bytes needed to encode string.", byteCount
);
}
}
Imports System
Imports System.Text
Class ASCIIEncodingExample
Public Shared Sub Main()
Dim chars As String = "ASCII Encoding Example"
Dim ascii As New ASCIIEncoding()
Dim byteCount As Integer = ascii.GetByteCount(chars)
Console.WriteLine("{0} bytes needed to encode string.", byteCount)
End Sub
End Class
Remarks
To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.
GetByteCount(Char*, Int32)
This API is not CLS-compliant.
- CLS-compliant alternative
- System.Text.ASCIIEncoding.GetByteCount(Char[], Int32, Int32)
Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public override int GetByteCount (char* chars, int count);
- chars
- Char*
A pointer to the first character to encode.
- count
- Int32
The number of characters to encode.
The number of bytes produced by encoding the specified characters.
chars is null.
count is less than zero.
-or-
The resulting number of bytes is greater than the maximum number that can be returned as an integer.
A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)
-and-
EncoderFallback is set to EncoderExceptionFallback.
Remarks
To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.
GetByteCount(Char[], Int32, Int32)
Calculates the number of bytes produced by encoding a set of characters from the specified character array.
public override int GetByteCount (char[] chars, int index, int count);
- chars
- Char[]
The character array containing the set of characters to encode.
- index
- Int32
The index of the first character to encode.
- count
- Int32
The number of characters to encode.
The number of bytes produced by encoding the specified characters.
chars is null.
index or count is less than zero.
-or-
index and count do not denote a valid range in chars.
-or-
The resulting number of bytes is greater than the maximum number that can be returned as an integer.
A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)
-and-
EncoderFallback is set to EncoderExceptionFallback.
Examples
The following example demonstrates how to use the GetByteCount method to return the number of bytes required to encode an array of Unicode characters using ASCIIEncoding.
using namespace System;
using namespace System::Text;
int main()
{
// Unicode characters.
// Pi
// Sigma
array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
int byteCount = ascii->GetByteCount( chars, 1, 2 );
Console::WriteLine( " {0} bytes needed to encode characters.", byteCount.ToString() );
}
using System;
using System.Text;
class ASCIIEncodingExample {
public static void Main() {
// Unicode characters.
Char[] chars = new Char[] {
'\u0023', // #
'\u0025', // %
'\u03a0', // Pi
'\u03a3' // Sigma
};
ASCIIEncoding ascii = new ASCIIEncoding();
int byteCount = ascii.GetByteCount(chars, 1, 2);
Console.WriteLine(
"{0} bytes needed to encode characters.", byteCount
);
}
}
Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class ASCIIEncodingExample
Public Shared Sub Main()
' 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 ascii As New ASCIIEncoding()
Dim byteCount As Integer = ascii.GetByteCount(chars, 1, 2)
Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
End Sub
End Class
Remarks
To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.