ASCIIEncoding.GetByteCount Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Calculates the number of bytes produced by encoding a set of characters.
Overloads
GetByteCount(ReadOnlySpan<Char>) |
Calculates the number of bytes produced by encoding the specified character span. |
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(ReadOnlySpan<Char>)
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
Calculates the number of bytes produced by encoding the specified character span.
public:
override int GetByteCount(ReadOnlySpan<char> chars);
public override int GetByteCount (ReadOnlySpan<char> chars);
override this.GetByteCount : ReadOnlySpan<char> -> int
Public Overrides Function GetByteCount (chars As ReadOnlySpan(Of Char)) As Integer
Parameters
- chars
- ReadOnlySpan<Char>
The span that contains the set of characters to encode.
Returns
The number of bytes produced by encoding the specified character span.
Remarks
To calculate the exact size required by GetBytes to store the resulting bytes, use GetByteCount. To calculate the maximum size, use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.
Applies to
GetByteCount(String)
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
Calculates the number of bytes produced by encoding the characters in the specified String.
public:
override int GetByteCount(System::String ^ chars);
public override int GetByteCount (string chars);
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (chars As String) As Integer
Parameters
Returns
The number of bytes produced by encoding the specified characters.
Exceptions
chars
is null
.
The resulting number of bytes is greater than the maximum number that can be returned as an integer.
A fallback occurred (for more information, see Character Encoding in .NET)
-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.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.
See also
- GetBytes(String, Int32, Int32, Byte[], Int32)
- GetMaxByteCount(Int32)
- GetEncoder()
- Understanding Encodings
Applies to
GetByteCount(Char*, Int32)
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
Important
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.
public:
override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
Parameters
- chars
- Char*
A pointer to the first character to encode.
- count
- Int32
The number of characters to encode.
Returns
The number of bytes produced by encoding the specified characters.
- Attributes
Exceptions
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 (for more information, see Character Encoding in .NET)
-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.
See also
Applies to
GetByteCount(Char[], Int32, Int32)
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
- Source:
- ASCIIEncoding.cs
Calculates the number of bytes produced by encoding a set of characters from the specified character array.
public:
override int GetByteCount(cli::array <char> ^ chars, int index, int count);
public override int GetByteCount (char[] chars, int index, int count);
override this.GetByteCount : char[] * int * int -> int
Public Overrides Function GetByteCount (chars As Char(), index As Integer, count As Integer) As Integer
Parameters
- 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.
Returns
The number of bytes produced by encoding the specified characters.
Exceptions
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 (for more information, see Character Encoding in .NET)
-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.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.