Byte.ToString 方法

定義

將目前 Byte 物件的值,轉換為其相等的字串表示。

多載

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將目前 Byte 物件的數值轉換為其相等的字串表示。

ToString(String)

使用指定的格式,將目前 Byte 物件的值,轉換為其相等字串表示。

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性的格式資訊,將目前 Byte 物件的值,轉換為其相等的字串表示。

ToString()

將目前 Byte 物件的值,轉換為其相等的字串表示。

ToString(IFormatProvider)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

使用指定的特定文化特性格式資訊,將目前 Byte 物件的數值轉換為其相等的字串表示。

public:
 virtual System::String ^ ToString(IFormatProvider ^ provider);
public:
 System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider

物件,提供特定文化特性格式資訊。

傳回

這個物件值的字串表示,採用 provider 參數所指定的格式。

實作

範例

下列範例會逐一查看位元組值的陣列,並使用不同的格式提供者呼叫 ToString(IFormatProvider) 方法,以將每個值顯示給主控台。

array<Byte>^ bytes = gcnew array<Byte> {0, 1, 14, 168, 255};
array<CultureInfo^>^ providers = {gcnew CultureInfo("en-us"), 
                                  gcnew CultureInfo("fr-fr"), 
                                  gcnew CultureInfo("de-de"), 
                                  gcnew CultureInfo("es-es")};
for each (Byte byteValue in bytes)
{
   for each (CultureInfo^ provider in providers)
      Console::Write("{0,3} ({1})      ", 
                    byteValue.ToString(provider), provider->Name);

   Console::WriteLine();                                        
}
// The example displays the following output to the console:
//      0 (en-US)        0 (fr-FR)        0 (de-DE)        0 (es-ES)
//      1 (en-US)        1 (fr-FR)        1 (de-DE)        1 (es-ES)
//     14 (en-US)       14 (fr-FR)       14 (de-DE)       14 (es-ES)
//    168 (en-US)      168 (fr-FR)      168 (de-DE)      168 (es-ES)
//    255 (en-US)      255 (fr-FR)      255 (de-DE)      255 (es-ES)
byte[] bytes = {0, 1, 14, 168, 255};
CultureInfo[] providers = {new CultureInfo("en-us"),
                           new CultureInfo("fr-fr"),
                           new CultureInfo("de-de"),
                           new CultureInfo("es-es")};
foreach (byte byteValue in bytes)
{
   foreach (CultureInfo provider in providers)
      Console.Write("{0,3} ({1})      ",
                    byteValue.ToString(provider), provider.Name);

   Console.WriteLine();
}
// The example displays the following output to the console:
//      0 (en-US)        0 (fr-FR)        0 (de-DE)        0 (es-ES)
//      1 (en-US)        1 (fr-FR)        1 (de-DE)        1 (es-ES)
//     14 (en-US)       14 (fr-FR)       14 (de-DE)       14 (es-ES)
//    168 (en-US)      168 (fr-FR)      168 (de-DE)      168 (es-ES)
//    255 (en-US)      255 (fr-FR)      255 (de-DE)      255 (es-ES)
let bytes = [| 0; 1; 14; 168; 255 |]
let providers = 
    [ CultureInfo "en-us"
      CultureInfo "fr-fr"
      CultureInfo "de-de"
      CultureInfo "es-es" ]

for byteValue in bytes do
    for provider in providers do
        printf $"{byteValue.ToString provider,3} ({provider.Name})      " 

    printfn ""

// The example displays the following output to the console:
//      0 (en-US)        0 (fr-FR)        0 (de-DE)        0 (es-ES)
//      1 (en-US)        1 (fr-FR)        1 (de-DE)        1 (es-ES)
//     14 (en-US)       14 (fr-FR)       14 (de-DE)       14 (es-ES)
//    168 (en-US)      168 (fr-FR)      168 (de-DE)      168 (es-ES)
//    255 (en-US)      255 (fr-FR)      255 (de-DE)      255 (es-ES)
Dim bytes() As Byte = {0, 1, 14, 168, 255}
Dim providers() As CultureInfo = {New CultureInfo("en-us"), _
                                  New CultureInfo("fr-fr"), _
                                  New CultureInfo("de-de"), _
                                  New CultureInfo("es-es")}
For Each byteValue As Byte In bytes
   For Each provider As CultureInfo In providers
      Console.Write("{0,3} ({1})      ", byteValue.ToString(provider), provider.Name)
   Next
   Console.WriteLine()                                        
Next
' The example displays the following output to the console:
'      0 (en-US)        0 (fr-FR)        0 (de-DE)        0 (es-ES)
'      1 (en-US)        1 (fr-FR)        1 (de-DE)        1 (es-ES)
'     14 (en-US)       14 (fr-FR)       14 (de-DE)       14 (es-ES)
'    168 (en-US)      168 (fr-FR)      168 (de-DE)      168 (es-ES)
'    255 (en-US)      255 (fr-FR)      255 (de-DE)      255 (es-ES)

備註

傳回值會以一般數值格式標準格式化, (「G」) 。

參數 provider 是實作 IFormatProvider 介面的物件。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供這個方法所傳回字串格式的特定文化特性資訊。 實作 IFormatProvider 的物件可以是下列任一項:

如果 是 nullproviderNumberFormatInfo 物件無法從 provider 取得,則傳回值會使用 NumberFormatInfo 執行緒目前文化特性的物件來格式化。 如需執行緒目前文化特性的相關資訊,請參閱 Thread.CurrentCulture

.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明:

另請參閱

適用於

ToString(String)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

使用指定的格式,將目前 Byte 物件的值,轉換為其相等字串表示。

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

數值格式字串。

傳回

目前 Byte 物件的字串表示,按照 format 參數所指定來格式化。

例外狀況

format 包含不支援的規範。 支援的格式規範列在<備註>一節。

範例

下列範例會 Byte 初始化 值,並使用每個支援的標準格式字串和自訂格式字串,向主控台顯示該值。 此範例是以 en-US 作為目前文化特性來執行。

array<String^>^ formats = gcnew array<String^> {"C3", "D4", "e1", "E2", "F1", "G", "N1", 
                                                "P0", "X4", "0000.0000"};
Byte number = 240;
for each (String^ format in formats)
   Console::WriteLine("'{0}' format specifier: {1}", 
                     format, number.ToString(format));

// The example displays the following output to the console if the
// current culture is en-us:
//       'C3' format specifier: $240.000
//       'D4' format specifier: 0240
//       'e1' format specifier: 2.4e+002
//       'E2' format specifier: 2.40E+002
//       'F1' format specifier: 240.0
//       'G' format specifier: 240
//       'N1' format specifier: 240.0
//       'P0' format specifier: 24,000 %
//       'X4' format specifier: 00F0
//       '0000.0000' format specifier: 0240.0000
string[] formats = {"C3", "D4", "e1", "E2", "F1", "G", "N1",
                    "P0", "X4", "0000.0000"};
byte number = 240;
foreach (string format in formats)
   Console.WriteLine("'{0}' format specifier: {1}",
                     format, number.ToString(format));

// The example displays the following output to the console if the
// current culture is en-us:
//       'C3' format specifier: $240.000
//       'D4' format specifier: 0240
//       'e1' format specifier: 2.4e+002
//       'E2' format specifier: 2.40E+002
//       'F1' format specifier: 240.0
//       'G' format specifier: 240
//       'N1' format specifier: 240.0
//       'P0' format specifier: 24,000 %
//       'X4' format specifier: 00F0
//       '0000.0000' format specifier: 0240.0000
let formats = 
    [ "C3"; "D4"; "e1"; "E2"; "F1"; "G"; "N1"
      "P0"; "X4"; "0000.0000" ]
let number = 240uy
for format in formats do
    printfn $"'{format}' format specifier: {number.ToString format}"

// The example displays the following output to the console if the
// current culture is en-us:
//       'C3' format specifier: $240.000
//       'D4' format specifier: 0240
//       'e1' format specifier: 2.4e+002
//       'E2' format specifier: 2.40E+002
//       'F1' format specifier: 240.0
//       'G' format specifier: 240
//       'N1' format specifier: 240.0
//       'P0' format specifier: 24,000 %
//       'X4' format specifier: 00F0
//       '0000.0000' format specifier: 0240.0000
Dim formats() As String = {"C3", "D4", "e1", "E2", "F1", "G", _
                           "N1", "P0", "X4", "0000.0000"}
Dim number As Byte = 240
For Each format As String In formats
   Console.WriteLine("'{0}' format specifier: {1}", _
                     format, number.ToString(format))
Next  
' The example displays the following output to the console if the
' current culture is en-us:
'       'C3' format specifier: $240.000
'       'D4' format specifier: 0240
'       'e1' format specifier: 2.4e+002
'       'E2' format specifier: 2.40E+002
'       'F1' format specifier: 240.0       
'       'G' format specifier: 240
'       'N1' format specifier: 240.0
'       'P0' format specifier: 24,000 %
'       'X4' format specifier: 00F0
'       '0000.0000' format specifier: 0240.0000

備註

參數 format 可以是標準或自訂數值格式字串。 支援 「R」 (或 「r」 ) 以外的所有標準數值格式字串,如同所有自訂數值格式字元一樣。 如果 為 formatnull 或空字串 (「」) ,則傳回值會以一般數值格式標準格式化, (「G」) 。

這個函式的傳回值會使用 NumberFormatInfo 執行緒目前文化特性的物件來格式化。 如需執行緒目前文化特性的相關資訊,請參閱 Thread.CurrentCulture 。 若要為目前文化特性以外的文化特性提供格式資訊,請呼叫 Byte.ToString(String, IFormatProvider) 方法。

.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明:

另請參閱

適用於

ToString(String, IFormatProvider)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

使用指定的格式和特定文化特性的格式資訊,將目前 Byte 物件的值,轉換為其相等的字串表示。

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string format, IFormatProvider provider);
public string ToString (string? format, IFormatProvider? provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

參數

format
String

標準或自訂數值格式字串。

provider
IFormatProvider

物件,提供特定文化特性格式資訊。

傳回

目前 Byte 物件的字串表示,按照 formatprovider 參數所指定來格式化。

實作

例外狀況

format 包含不支援的規範。 支援的格式規範列在<備註>一節。

範例

下列範例會使用標準 「N」 格式字串和四個不同的 CultureInfo 物件,將位元組值的字串表示顯示給主控台。

Byte byteValue = 250;
array<CultureInfo^>^ providers = gcnew array<CultureInfo^> { gcnew CultureInfo("en-us"), 
                                                             gcnew CultureInfo("fr-fr"), 
                                                             gcnew CultureInfo("es-es"), 
                                                             gcnew CultureInfo("de-de")}; 

for each (CultureInfo^ provider in providers) 
   Console::WriteLine("{0} ({1})", 
                     byteValue.ToString("N2", provider), provider->Name);
// The example displays the following output to the console:
//       250.00 (en-US)
//       250,00 (fr-FR)
//       250,00 (es-ES)
//       250,00 (de-DE)
byte byteValue = 250;
CultureInfo[] providers = {new CultureInfo("en-us"),
                           new CultureInfo("fr-fr"),
                           new CultureInfo("es-es"),
                           new CultureInfo("de-de")};

foreach (CultureInfo provider in providers)
   Console.WriteLine("{0} ({1})",
                     byteValue.ToString("N2", provider), provider.Name);
// The example displays the following output to the console:
//       250.00 (en-US)
//       250,00 (fr-FR)
//       250,00 (es-ES)
//       250,00 (de-DE)
let byteValue = 250uy
let providers = 
    [ CultureInfo "en-us"
      CultureInfo "fr-fr"
      CultureInfo "es-es"
      CultureInfo "de-de" ]

for provider in providers do
    printfn $"""{byteValue.ToString("N2", provider)} ({provider.Name})"""

// The example displays the following output to the console:
//       250.00 (en-US)
//       250,00 (fr-FR)
//       250,00 (es-ES)
//       250,00 (de-DE)
Dim byteValue As Byte = 250
Dim providers() As CultureInfo = {New CultureInfo("en-us"), _
                                  New CultureInfo("fr-fr"), _
                                  New CultureInfo("es-es"), _
                                  New CultureInfo("de-de")} 
For Each provider As CultureInfo In providers 
   Console.WriteLine("{0} ({1})", _
                     byteValue.ToString("N2", provider), provider.Name)
Next   
' The example displays the following output to the console:
'       250.00 (en-US)
'       250,00 (fr-FR)
'       250,00 (es-ES)
'       250,00 (de-DE)

備註

方法會 ToString(String, IFormatProvider) 以指定文化特性的指定格式格式化 Byte 值。 若要使用目前文化特性的預設 (「G」) 格式來格式化數位,請呼叫 ToString() 方法。 若要使用目前文化特性的指定格式來格式化數位,請呼叫 ToString(String) 方法。

參數 format 可以是標準或自訂數值格式字串。 支援 「R」 (或 「r」 ) 以外的所有標準數值格式字串,如同所有自訂數值格式字元一樣。 如果 為 formatnull 或空字串 (「」「) ,則此方法的傳回值會以一般數值格式標準格式化 (」G「) 。

參數 provider 是實作 IFormatProvider 介面的物件。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供這個方法所傳回字串格式的特定文化特性資訊。 實作 IFormatProvider 的物件可以是下列任一項:

如果 是 nullproviderNumberFormatInfo 物件無法從 provider 取得,則傳回值會使用 NumberFormatInfo 執行緒目前文化特性的物件來格式化。 如需執行緒目前文化特性的相關資訊,請參閱 Thread.CurrentCulture

.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明:

另請參閱

適用於

ToString()

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

將目前 Byte 物件的值,轉換為其相等的字串表示。

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

這個物件值的字串表示,由沒有零的前置字元且範圍從 0 到 9 的數字順序所組成。

範例

下列範例會顯示位元組值的陣列。 請注意,此 ToString() 方法不會在範例中明確呼叫。 相反地,它會隱含地呼叫,因為使用 複合格式 設定功能,F# 範例會使用 字串插補

array<Byte>^ bytes = gcnew array<Byte> {0, 1, 14, 168, 255};
for each (Byte byteValue in bytes)
   Console::WriteLine(byteValue);
// The example displays the following output to the console if the current
// culture is en-US:
//       0
//       1
//       14
//       168
//       255
byte[] bytes = {0, 1, 14, 168, 255};
foreach (byte byteValue in bytes)
   Console.WriteLine(byteValue);
// The example displays the following output to the console if the current
// culture is en-US:
//       0
//       1
//       14
//       168
//       255
let bytes = [| 0; 1; 14; 168; 255 |]
for byteValue in bytes do
    printfn $"{byteValue}"

// The example displays the following output to the console if the current
// culture is en-US:
//       0
//       1
//       14
//       168
//       255
Dim bytes() As Byte = {0, 1, 14, 168, 255}
For Each byteValue As Byte In Bytes
   Console.WriteLine(byteValue)
Next   
' The example displays the following output to the console if the current
' culture is en-US:
'       0
'       1
'       14
'       168
'       255

備註

傳回值會使用一般數值格式規範來格式化, (「G」) 和 NumberFormatInfo 執行緒目前文化特性的物件。 若要定義 Byte 值字串表示的格式設定,請呼叫 ToString 方法。 若要定義用來建立值字串表示 Byte 的格式規範和文化特性,請呼叫 ToString 方法。

.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明:

如需執行緒目前文化特性的相關資訊,請參閱 Thread.CurrentCulture

適用於