本文整理汇总了C#中System.Text.Encoding.UTF8属性的典型用法代码示例。如果您正苦于以下问题:C# Encoding.UTF8属性的具体用法?C# Encoding.UTF8怎么用?C# Encoding.UTF8使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.UTF8属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Text;
public class Example
{
public static void Main()
{
// Create a character array.
string gkNumber = Char.ConvertFromUtf32(0x10154);
char[] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2',
gkNumber[0], gkNumber[1] };
// Get UTF-8 and UTF-16 encoders.
Encoding utf8 = Encoding.UTF8;
Encoding utf16 = Encoding.Unicode;
// Display the original characters' code units.
Console.WriteLine("Original UTF-16 code units:");
byte[] utf16Bytes = utf16.GetBytes(chars);
foreach (var utf16Byte in utf16Bytes)
Console.Write("{0:X2} ", utf16Byte);
Console.WriteLine();
// Display the number of bytes required to encode the array.
int reqBytes = utf8.GetByteCount(chars);
Console.WriteLine("\nExact number of bytes required: {0}",
reqBytes);
// Display the maximum byte count.
int maxBytes = utf8.GetMaxByteCount(chars.Length);
Console.WriteLine("Maximum number of bytes required: {0}\n",
maxBytes);
// Encode the array of chars.
byte[] utf8Bytes = utf8.GetBytes(chars);
// Display all the UTF-8-encoded bytes.
Console.WriteLine("UTF-8-encoded code units:");
foreach (var utf8Byte in utf8Bytes)
Console.Write("{0:X2} ", utf8Byte);
Console.WriteLine();
}
}
输出:
Original UTF-16 code units: 7A 00 61 00 06 03 FD 01 B2 03 00 D8 54 DD Exact number of bytes required: 12 Maximum number of bytes required: 24 UTF-8-encoded code units: 7A 61 CC 86 C7 BD CE B2 F0 90 85 94
示例2: Main
//引入命名空间
using System;
using System.Text;
public class Example
{
public static void Main()
{
Encoding enc = new UTF8Encoding(true, true);
string value = "\u00C4 \uD802\u0033 \u00AE";
try {
byte[] bytes= enc.GetBytes(value);
foreach (var byt in bytes)
Console.Write("{0:X2} ", byt);
Console.WriteLine();
string value2 = enc.GetString(bytes);
Console.WriteLine(value2);
}
catch (EncoderFallbackException e) {
Console.WriteLine("Unable to encode {0} at index {1}",
e.IsUnknownSurrogate() ?
String.Format("U+{0:X4} U+{1:X4}",
Convert.ToUInt16(e.CharUnknownHigh),
Convert.ToUInt16(e.CharUnknownLow)) :
String.Format("U+{0:X4}",
Convert.ToUInt16(e.CharUnknown)),
e.Index);
}
}
}
输出:
Unable to encode U+D802 at index 2
示例3: Main
//引入命名空间
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
using (StreamWriter output = new StreamWriter("practice.txt"))
{
// Create and write a string containing the symbol for Pi.
string srcString = "Area = \u03A0r^2";
// Convert the UTF-16 encoded source string to UTF-8 and ASCII.
byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
// Write the UTF-8 and ASCII encoded byte arrays.
output.WriteLine("UTF-8 Bytes: {0}", BitConverter.ToString(utf8String));
output.WriteLine("ASCII Bytes: {0}", BitConverter.ToString(asciiString));
Console.WriteLine(BitConverter.ToString(utf8String));
Console.WriteLine(BitConverter.ToString(asciiString));
}
}
}