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

Description

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

Syntax

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


public override int GetBytes(
  string s,/*from www.j  a v a 2  s . c  o m*/
  int charIndex,
  int charCount,
  byte[] bytes,
  int byteIndex
)

Parameters

UTF8Encoding.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

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

Example


using System;/*from   w  w w  . j  av a 2 s .  c om*/
using System.Text;

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

        UTF8Encoding utf8 = new UTF8Encoding();

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

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

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding