Byte.ToString 方法

定义

将当前 Byte 对象的值转换为其等效的字符串表示形式。

重载

名称 说明
ToString()

将当前 Byte 对象的值转换为其等效的字符串表示形式。

ToString(IFormatProvider)

使用指定的区域性特定格式设置信息将当前 Byte 对象的数值转换为其等效的字符串表示形式。

ToString(String)

使用指定的格式将当前 Byte 对象的值转换为其等效的字符串表示形式。

ToString(String, IFormatProvider)

使用指定的格式和区域性特定的格式设置信息将当前 Byte 对象的值转换为其等效的字符串表示形式。

ToString()

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
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# 示例使用 字符串内插,因此会隐式调用它。

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

适用于

ToString(IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
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) 具有不同格式提供程序的方法向控制台显示每个字节值。

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 的对象可以是以下任一对象:

如果provider无法从null中获取对象NumberFormatInfoprovider,则返回值使用NumberFormatInfo线程当前区域性的对象进行格式化。 有关线程当前区域性的信息,请参阅 Thread.CurrentCulture

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的描述:

另请参阅

适用于

ToString(String)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
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 作为当前区域性运行。

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)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
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 对象的字符串表示形式,格式由 format 参数 provider 指定。

实现

例外

format 包括不受支持的说明符。 “备注”部分列出了支持的格式说明符。

示例

以下示例使用标准“N”格式字符串和四个不同的 CultureInfo 对象向控制台显示字节值的字符串表示形式。

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 的对象可以是以下任一对象:

如果provider无法从null中获取对象NumberFormatInfoprovider,则返回值使用NumberFormatInfo线程当前区域性的对象进行格式化。 有关线程当前区域性的信息,请参阅 Thread.CurrentCulture

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的描述:

另请参阅

适用于