GetBytes Method (Char*, Int32, Byte*, Int32, Boolean)

Encoder.GetBytes Method (Char*, Int32, Byte*, Int32, Boolean)

When overridden in a derived class, encodes a set of characters starting at the specified character pointer and any characters in the internal buffer into a sequence of bytes that are stored starting at the specified byte pointer. A parameter indicates whether to clear the internal state of the encoder after the conversion.

This method is not CLS-compliant.  

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

public virtual int GetBytes (
	char* chars,
	int charCount,
	byte* bytes,
	int byteCount,
	bool flush
Not applicable.



A pointer to the first character to encode.


The number of characters to encode.


A pointer to the location at which to start writing the resulting sequence of bytes.


The maximum number of bytes to write.


true to clear the internal state of the encoder after the conversion; otherwise, false.

Return Value

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

Exception typeCondition


chars is a null reference (Nothing in Visual Basic)(Nothing).


bytes is a null reference (Nothing in Visual Basic)(Nothing).


charCount or byteCount is less than zero.


byteCount is less than the resulting number of bytes.


A fallback occurred (see Understanding Encodings for fuller explanation)


Fallback is set to EncoderExceptionFallback.

Remember that the Encoder object saves state between calls to GetBytes. When the application is done with a stream of data, it should set the flush parameter to true in the last call to GetBytes to make sure that the state information is flushed and that the encoded bytes are properly terminated. With this setting, the encoder ignores invalid bytes at the end of the data block, such as unmatched surrogates or incomplete combining sequences, and clears the internal buffer.

To calculate the exact buffer size that GetBytes requires to store the resulting characters, the application should use GetByteCount.

If GetBytes is called with flush set to false, the encoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next encoding operation. The application should call GetByteCount on a block of data immediately before calling GetBytes on the same block, so that any trailing characters from the previous block are included in the calculation.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

© 2015 Microsoft