C# UTF8Encoding UTF8Encoding(Boolean, Boolean)

Description

UTF8Encoding UTF8Encoding(Boolean, Boolean) Initializes a new instance of the UTF8Encoding class. Parameters specify whether to provide a Unicode byte order mark and whether to throw an exception when an invalid encoding is detected.

Syntax

UTF8Encoding.UTF8Encoding(Boolean, Boolean) has the following syntax.


public UTF8Encoding(
  bool encoderShouldEmitUTF8Identifier,
  bool throwOnInvalidBytes
)

Parameters

UTF8Encoding.UTF8Encoding(Boolean, Boolean) has the following parameters.

  • encoderShouldEmitUTF8Identifier - true to specify that a Unicode byte order mark is provided; otherwise, false.
  • throwOnInvalidBytes - true to specify that an exception be thrown when an invalid encoding is detected; otherwise, false.

Example


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

class UTF8EncodingExample
{
    public static void Main()
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true);
        Char[] chars = new Char[] { 'a', 'b', 'c', '\uD801', '\uD802', 'd' };
        Byte[] bytes = utf8.GetBytes(chars);
        ShowArray(bytes);

        bytes = utf8ThrowException.GetBytes(chars);
    }

    public static void ShowArray(Array theArray)
    {
        foreach (Object o in theArray)
        {
            Console.Write( o);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding