ASCIIEncoding.GetBytes
Method
Definition
Overloads
| GetBytes(Char*, Int32, Byte*, Int32) |
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(Char[], Int32, Int32, Byte[], Int32) |
Encodes a set of characters from the specified character array into the specified byte array. |
| GetBytes(String, Int32, Int32, Byte[], Int32) |
Encodes a set of characters from the specified String into the specified byte array. |
GetBytes(Char*, Int32, Byte*, Int32)
This API is not CLS-compliant.
- CLS-compliant alternative
- System.Text.ASCIIEncoding.GetBytes(Char[], Int32, Int32, Byte[], Int32)
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.
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);
- chars
- Char*
A pointer to the first character to encode.
- charCount
- Int32
The number of characters to encode.
- bytes
- Byte*
A pointer to the location at which to start writing the resulting sequence of bytes.
- byteCount
- Int32
The maximum number of bytes to write.
The actual number of bytes written at the location indicated by bytes.
charCount or byteCount is less than zero.
byteCount is less than the resulting number of bytes.
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.
Data to be converted, such as data read from a stream, can be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.
ASCIIEncoding does not provide error detection. Any Unicode character greater than U+007F is translated to an ASCII question mark ("?").
Caution
For security reasons, your application is recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.
GetBytes(Char[], Int32, Int32, Byte[], Int32)
Encodes a set of characters from the specified character array into the specified byte array.
public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);
- chars
- Char[]
The character array containing the set of characters to encode.
- charIndex
- Int32
The index of the first character to encode.
- charCount
- Int32
The number of characters to encode.
- bytes
- Byte[]
The byte array to contain the resulting sequence of bytes.
- byteIndex
- Int32
The index at which to start writing the resulting sequence of bytes.
The actual number of bytes written into bytes.
charIndex or charCount or byteIndex is less than zero.
-or-
charIndex and charCount do not denote a valid range in chars.
-or-
byteIndex is not a valid index in bytes.
bytes does not have enough capacity from byteIndex to the end of the array to accommodate the resulting bytes.
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 GetBytes method to encode a range of characters from a string and store the encoded characters in a range of elements in a byte array.
using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
array<Byte>^bytes;
String^ chars = "ASCII Encoding Example";
ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
int byteCount = ascii->GetByteCount( chars->ToCharArray(), 6, 8 );
bytes = gcnew array<Byte>(byteCount);
int bytesEncodedCount = ascii->GetBytes( chars, 6, 8, bytes, 0 );
Console::WriteLine( " {0} bytes used to encode string.", 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 ASCIIEncodingExample {
public static void Main() {
Byte[] bytes;
String chars = "ASCII Encoding Example";
ASCIIEncoding ascii = new ASCIIEncoding();
int byteCount = ascii.GetByteCount(chars.ToCharArray(), 6, 8);
bytes = new Byte[byteCount];
int bytesEncodedCount = ascii.GetBytes(chars, 6, 8, bytes, 0);
Console.WriteLine(
"{0} bytes used to encode string.", bytesEncodedCount
);
Console.Write("Encoded bytes: ");
foreach (Byte b in bytes) {
Console.Write("[{0}]", b);
}
Console.WriteLine();
}
}
Imports System
Imports System.Text
Class ASCIIEncodingExample
Public Shared Sub Main()
Dim bytes() As Byte
Dim chars As String = "ASCII Encoding Example"
Dim ascii As New ASCIIEncoding()
Dim byteCount As Integer = ascii.GetByteCount(chars.ToCharArray(), 6, 8)
bytes = New Byte(byteCount - 1) {}
Dim bytesEncodedCount As Integer = ascii.GetBytes(chars, 6, 8, bytes, 0)
Console.WriteLine("{0} bytes used to encode string.", 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
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.
Data to be converted, such as data read from a stream, can be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.
ASCIIEncoding does not provide error detection. Any Unicode character greater than U+007F is encoded as the ASCII question mark ("?").
Caution
For security reasons, your application is recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.
GetBytes(String, Int32, Int32, Byte[], Int32)
Encodes a set of characters from the specified String into the specified byte array.
public override int GetBytes (string chars, int charIndex, int charCount, byte[] bytes, int byteIndex);
- charIndex
- Int32
The index of the first character to encode.
- charCount
- Int32
The number of characters to encode.
- bytes
- Byte[]
The byte array to contain the resulting sequence of bytes.
- byteIndex
- Int32
The index at which to start writing the resulting sequence of bytes.
The actual number of bytes written into bytes.
charIndex or charCount or byteIndex is less than zero.
-or-
charIndex and charCount do not denote a valid range in chars.
-or-
byteIndex is not a valid index in bytes.
bytes does not have enough capacity from byteIndex to the end of the array to accommodate the resulting bytes.
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 GetBytes method to encode a range of elements from a Unicode character array and store the encoded bytes in a range of elements in a byte array.
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'};
ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
int byteCount = ascii->GetByteCount( chars, 1, 2 );
bytes = gcnew array<Byte>(byteCount);
int bytesEncodedCount = ascii->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 ASCIIEncodingExample {
public static void Main() {
Byte[] bytes;
// Unicode characters.
Char[] chars = new Char[] {
'\u0023', // #
'\u0025', // %
'\u03a0', // Pi
'\u03a3' // Sigma
};
ASCIIEncoding ascii = new ASCIIEncoding();
int byteCount = ascii.GetByteCount(chars, 1, 2);
bytes = new Byte[byteCount];
int bytesEncodedCount = ascii.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
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class ASCIIEncodingExample
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 ascii As New ASCIIEncoding()
Dim byteCount As Integer = ascii.GetByteCount(chars, 1, 2)
bytes = New Byte(byteCount - 1){}
Dim bytesEncodedCount As Integer = ascii.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
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.
Data to be converted, such as data read from a stream, can be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.
ASCIIEncoding does not provide error detection. Any Unicode character greater than U+007F is encoded as the ASCII question mark ("?").
Caution
For security reasons, your application is recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.