C# UnicodeEncoding GetBytes(Char[], Int32, Int32, Byte[], Int32)

Description

UnicodeEncoding GetBytes(Char[], Int32, Int32, Byte[], Int32) Encodes a set of characters from the specified character array into the specified byte array.

Syntax

UnicodeEncoding.GetBytes(Char[], Int32, Int32, Byte[], Int32) has the following syntax.


public override int GetBytes(
  char[] chars,/*w w  w  .j  a  va 2 s  .com*/
  int charIndex,
  int charCount,
  byte[] bytes,
  int byteIndex
)

Parameters

UnicodeEncoding.GetBytes(Char[], Int32, Int32, Byte[], Int32) has the following parameters.

  • chars - The character array containing the set of characters to encode.
  • charIndex - The index of the first character to encode.
  • charCount - The number of characters to encode.
  • bytes - The byte array to contain the resulting sequence of bytes.
  • byteIndex - The index at which to start writing the resulting sequence of bytes.

Returns

UnicodeEncoding.GetBytes(Char[], Int32, Int32, Byte[], Int32) method returns The actual number of bytes written into bytes.

Example

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.


//from  w ww  .java  2 s  .  c  o  m
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Byte[] bytes;
        String chars = "Unicode Encoding Example";

        UnicodeEncoding Unicode = new UnicodeEncoding();

        int byteCount = Unicode.GetByteCount(chars.ToCharArray(), 8, 8);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = Unicode.GetBytes(chars, 8, 8, bytes, 0);

        Console.WriteLine(bytesEncodedCount);

        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding