Encoding.UTF8 屬性(System.Text) - Microsoft Learn
文章推薦指數: 80 %
WriteLine(); // Display the number of bytes required to encode the array. int reqBytes = utf8.GetByteCount(chars); Console.WriteLine("\nExact number of ...
跳到主要內容
已不再支援此瀏覽器。
請升級至MicrosoftEdge,以利用最新功能、安全性更新和技術支援。
下載MicrosoftEdge
InternetExplorer和MicrosoftEdge的詳細資訊
目錄
結束焦點模式
語言
閱讀英文
儲存
目錄
閱讀英文
儲存
編輯
Twitter
LinkedIn
Facebook
電子郵件
目錄
Encoding.UTF8屬性
參考
定義
命名空間:
System.Text
組件:System.Text.Encoding.dll
組件:System.Runtime.dll
組件:netstandard.dll
組件:mscorlib.dll
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。
Microsoft對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
本文內容
取得UTF-8格式的編碼方式。
public:
staticpropertySystem::Text::Encoding^UTF8{System::Text::Encoding^get();};
publicstaticSystem.Text.EncodingUTF8{get;}
memberthis.UTF8:System.Text.Encoding
PublicSharedReadOnlyPropertyUTF8AsEncoding
屬性值
Encoding
UTF-8格式的編碼方式。
範例
下列範例會定義包含下列字元的陣列:
拉丁小寫字母Z(U+007A)
拉丁小寫字母A(U+0061)
結合短(U+0306)
使用銳角(U+01FD)的拉丁小寫字母AE
希臘文小寫字母BETA(U+03B2)
(U+D800U+DD54)的代理配對,可形成希臘文ACROPHONIC閣樓1000STATERS(U+10154)。
它會顯示每個字元的UTF-16程式碼單位,並決定UTF-8編碼器為字元陣列編碼所需的位元組數目。
然後,它會將字元編碼,並顯示產生的UTF-8編碼位元組。
usingSystem;
usingSystem.Text;
publicclassExample
{
publicstaticvoidMain()
{
//Createacharacterarray.
stringgkNumber=Char.ConvertFromUtf32(0x10154);
char[]chars=newchar[]{'z','a','\u0306','\u01FD','\u03B2',
gkNumber[0],gkNumber[1]};
//GetUTF-8andUTF-16encoders.
Encodingutf8=Encoding.UTF8;
Encodingutf16=Encoding.Unicode;
//Displaytheoriginalcharacters'codeunits.
Console.WriteLine("OriginalUTF-16codeunits:");
byte[]utf16Bytes=utf16.GetBytes(chars);
foreach(varutf16Byteinutf16Bytes)
Console.Write("{0:X2}",utf16Byte);
Console.WriteLine();
//Displaythenumberofbytesrequiredtoencodethearray.
intreqBytes=utf8.GetByteCount(chars);
Console.WriteLine("\nExactnumberofbytesrequired:{0}",
reqBytes);
//Displaythemaximumbytecount.
intmaxBytes=utf8.GetMaxByteCount(chars.Length);
Console.WriteLine("Maximumnumberofbytesrequired:{0}\n",
maxBytes);
//Encodethearrayofchars.
byte[]utf8Bytes=utf8.GetBytes(chars);
//DisplayalltheUTF-8-encodedbytes.
Console.WriteLine("UTF-8-encodedcodeunits:");
foreach(varutf8Byteinutf8Bytes)
Console.Write("{0:X2}",utf8Byte);
Console.WriteLine();
}
}
//Theexampledisplaysthefollowingoutput:
//OriginalUTF-16codeunits:
//7A0061000603FD01B20300D854DD
//
//Exactnumberofbytesrequired:12
//Maximumnumberofbytesrequired:24
//
//UTF-8-encodedcodeunits:
//7A61CC86C7BDCEB2F0908594
ImportsSystem.Text
PublicModuleExample
PublicSubMain()
'Createacharacterarray.
DimgkNumberAsString=Char.ConvertFromUtf32(&h10154)
Dimchars()AsChar={"z"c,"a"c,ChrW(&H0306),ChrW(&H01FD),
ChrW(&H03B2),gkNumber(0),gkNumber(1)}
'GetUTF-8andUTF-16encoders.
Dimutf8AsEncoding=Encoding.UTF8
Dimutf16AsEncoding=Encoding.Unicode
'Displaytheoriginalcharacters'codeunits.
Console.WriteLine("OriginalUTF-16codeunits:")
Dimutf16Bytes()AsByte=utf16.GetBytes(chars)
ForEachutf16ByteInutf16Bytes
Console.Write("{0:X2}",utf16Byte)
Next
Console.WriteLine()
Console.WriteLine()
'Displaythenumberofbytesrequiredtoencodethearray.
DimreqBytesAsInteger=utf8.GetByteCount(chars)
Console.WriteLine("Exactnumberofbytesrequired:{0}",
reqBytes)
'Displaythemaximumbytecount.
DimmaxBytesAsInteger=utf8.GetMaxByteCount(chars.Length)
Console.WriteLine("Maximumnumberofbytesrequired:{0}",
maxBytes)
Console.WriteLine()
'Encodethearrayofcharacters.
Dimutf8Bytes()AsByte=utf8.GetBytes(chars)
'DisplayalltheUTF-8-encodedbytes.
Console.WriteLine("UTF-8-encodedcodeunits:")
ForEachutf8ByteInutf8Bytes
Console.Write("{0:X2}",utf8Byte)
Next
Console.WriteLine()
EndSub
EndModule
'Theexampledisplaysthefollowingoutput:
'OriginalUTF-16codeunits:
'7A0061000603FD01B20300D854DD
'
'Exactnumberofbytesrequired:12
'Maximumnumberofbytesrequired:24
'
'UTF-8-encodedcodeunits:
'7A61CC86C7BDCEB2F0908594
備註
這個屬性會傳回UTF8Encoding物件,此物件會將Unicode(utf-16編碼的)字元編碼為每個字元的一到四個位元組序列,並將utf-8編碼的位元組陣列解碼為Unicode(utf-16編碼的)字元。
如需.NET所支援之字元編碼方式的相關資訊,以及要使用哪些Unicode編碼的討論,請參閱.net中的字元編碼。
UTF8Encoding這個屬性所傳回的物件可能沒有適用于您應用程式的適當行為。
它會傳回一個UTF8Encoding物件,該物件會提供(BOM)的Unicode位元組順序標記。
若要具現化未提供BOM的UTF8編碼,請呼叫此函式的任何多載UTF8Encoding。
它會傳回UTF8Encoding物件,該物件使用取代回取代來取代無法編碼的每個字串,而每個位元組無法以問號解碼("?")字元。
相反地,您可以呼叫此函UTF8Encoding.UTF8Encoding(Boolean,Boolean)式來具UTF8Encoding現化物件,而該物件的回復是EncoderFallbackException或DecoderFallbackException,如下列範例所示。
usingSystem;
usingSystem.Text;
publicclassExample
{
publicstaticvoidMain()
{
Encodingenc=newUTF8Encoding(true,true);
stringvalue="\u00C4\uD802\u0033\u00AE";
try{
byte[]bytes=enc.GetBytes(value);
foreach(varbytinbytes)
Console.Write("{0:X2}",byt);
Console.WriteLine();
stringvalue2=enc.GetString(bytes);
Console.WriteLine(value2);
}
catch(EncoderFallbackExceptione){
Console.WriteLine("Unabletoencode{0}atindex{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);
}
}
}
//Theexampledisplaysthefollowingoutput:
//UnabletoencodeU+D802atindex2
ImportsSystem.Text
ModuleExample
PublicSubMain()
DimencAsEncoding=NewUTF8Encoding(True,True)
DimvalueAsString=String.Format("{0}{1}{2}{3}",
ChrW(&h00C4),ChrW(&hD802),ChrW(&h0033),ChrW(&h00AE))
Try
Dimbytes()AsByte=enc.GetBytes(value)
ForEachbytAsByteInbytes
Console.Write("{0:X2}",byt)
Next
Console.WriteLine()
Dimvalue2AsString=enc.GetString(bytes)
Console.WriteLine(value2)
CatcheAsEncoderFallbackException
Console.WriteLine("Unabletoencode{0}atindex{1}",
If(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)
EndTry
EndSub
EndModule
'Theexampledisplaysthefollowingoutput:
'UnabletoencodeU+D802atindex2
適用於
另請參閱
UTF8Encoding
GetEncoding(Int32)
如何在.NET中使用字元編碼類別
本文內容
延伸文章資訊
- 1What is UTF-8? UTF-8 Character Encoding Tutorial
UTF-8 is a character encoding system. It lets you represent characters as ASCII text, while still...
- 2UTF-8 - Wikipedia
UTF-8 is a variable-width character encoding used for electronic communication. Defined by the Un...
- 3如何把純文字檔案轉換成UTF-8格式:使用Notepad++ / How ...
這就是Notepad++的畫面了。 Big5純文字範例檔案下載/ Download Big5 Encoding Text File. image. 有時候我們拿到的檔案並不是用UTF8編碼 ...
- 4What is UTF-8 Encoding? A Guide for Non-Programmers
UTF-8 is an encoding system for Unicode. It can translate any Unicode character to a matching uni...
- 5Day27 Python 基礎- 字符轉編碼操作 - iT 邦幫忙
coding:utf-8 -*- import sys print(sys.getdefaultencoding()) # 打印出目前系統字符編碼s = '你好' s_to_unicode = ...