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

Description

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

Syntax

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


public override int GetBytes(
  string s,/* ww w.ja v a2 s.c  o  m*/
  int charIndex,
  int charCount,
  byte[] bytes,
  int byteIndex
)

Parameters

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

  • s - The String 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(String, Int32, Int32, Byte[], Int32) method returns The actual number of bytes written into bytes.

Example

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


/*from w  w w .j  a  v a 2 s  . c  o  m*/
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Byte[] bytes;
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

        UnicodeEncoding Unicode = new UnicodeEncoding();

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

        Console.WriteLine(bytesEncodedCount);

        Console.Write("Encoded bytes: ");
        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