UTF7Encoding.GetBytes Method

Definition

Encodes a set of characters into a sequence of bytes.

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)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

Important

This API is not CLS-compliant.

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.

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
C#
[System.CLSCompliant(false)]
public override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
C#
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);

Parameters

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.

Returns

The actual number of bytes written at the location indicated by bytes.

Attributes

Exceptions

chars is null (Nothing).

-or-

bytes is null (Nothing).

charCount or byteCount is less than zero.

byteCount is less than the resulting number of bytes.

A fallback occurred (see Character Encoding in .NET for fuller 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, might 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.

Note

UTF7Encoding does not provide error detection. Invalid characters are encoded as a modified base 64 character. For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

Encodes a set of characters from the specified character array into the specified byte array.

C#
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);

Parameters

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.

Returns

The actual number of bytes written into bytes.

Exceptions

chars is null (Nothing).

-or-

bytes is null (Nothing).

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 .NET for fuller explanation).

-and-

EncoderFallback is set to EncoderExceptionFallback.

Examples

The following code example demonstrates how to use the GetBytes method to encode a range of characters from a String and store the encoded bytes in a range of elements in a byte array.

C#
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();
    }
}

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, might 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.

Note

UTF7Encoding does not provide error detection. Invalid characters are encoded as a modified base 64 character. For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

Encodes a set of characters from the specified String into the specified byte array.

C#
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex);

Parameters

s
String

The String 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.

Returns

The actual number of bytes written into bytes.

Attributes

Exceptions

s is null (Nothing).

-or-

bytes is null (Nothing).

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 .NET for fuller explanation).

-and-

EncoderFallback is set to EncoderExceptionFallback.

Examples

The following code 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.

C#
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();
    }
}

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, might 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.

Note

UTF7Encoding does not provide error detection. Invalid characters are encoded as a modified base 64 character. For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0