Byte.ToString 方法

定义

将当前 Byte 对象的值转换为其等效的字符串表示形式。Converts the value of the current Byte object to its equivalent string representation.

重载

ToString(IFormatProvider)

使用指定的区域性特定格式设置信息将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the numeric value of the current Byte object to its equivalent string representation using the specified culture-specific formatting information.

ToString(String, IFormatProvider)

使用指定的格式和区域性特定格式信息将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the value of the current Byte object to its equivalent string representation using the specified format and culture-specific formatting information.

ToString(String)

使用指定的格式将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the value of the current Byte object to its equivalent string representation using the specified format.

ToString()

将当前 Byte 对象的值转换为其等效的字符串表示形式。Converts the value of the current Byte object to its equivalent string representation.

ToString(IFormatProvider)

使用指定的区域性特定格式设置信息将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the numeric value of the current Byte object to its equivalent string representation using the specified culture-specific formatting information.

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

参数

provider
IFormatProvider

一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.

返回

此对象值的字符串表示形式,采用 provider 参数所指定的格式。The string representation of the value of this object in the format specified by the provider parameter.

实现

示例

下面的示例循环访问一个字节值数组,并通过使用不同的格式提供程序调用 ToString(IFormatProvider) 方法,将它们显示到控制台。The following example iterates an array of byte values and displays each of them to the console by calling the ToString(IFormatProvider) method with different format providers.

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)            
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")进行格式设置。The return value is formatted with the general numeric format specifier ("G").

provider 参数是实现 IFormatProvider 接口的对象。The provider parameter is an object that implements the IFormatProvider interface. 它的 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. 实现 IFormatProvider 的对象可以是以下任一项:The object that implements IFormatProvider can be any of the following:

  • 一个表示要使用其格式设置规则的区域性的 CultureInfo 对象。A CultureInfo object that represents the culture whose formatting rules are to be used.

  • 一个包含此值的特定数字格式信息的 NumberFormatInfo 对象。A NumberFormatInfo object that contains specific numeric formatting information for this value.

  • 一个实现 IFormatProvider的自定义对象。A custom object that implements IFormatProvider.

如果 provider null 或无法从 provider获取 NumberFormatInfo 对象,则使用当前区域性的 NumberFormatInfo 对象对返回值进行格式设置。If provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value is formatted using the NumberFormatInfo object for the thread current culture. 有关线程的当前区域性的信息,请参阅 Thread.CurrentCultureFor information about the thread current culture, see Thread.CurrentCulture.

.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

另请参阅

ToString(String, IFormatProvider)

使用指定的格式和区域性特定格式信息将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the value of the current Byte object to its equivalent string representation using the specified format and culture-specific formatting information.

public:
 virtual System::String ^ ToString(System::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

标准或自定义的数值格式字符串。A standard or custom numeric format string.

provider
IFormatProvider

一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.

返回

按照 Byteformat 参数指定的方式进行格式设置的当前 provider 对象的字符串表示形式。The string representation of the current Byte object, formatted as specified by the format and provider parameters.

实现

异常

format 包含不受支持的说明符。format includes an unsupported specifier. “备注”部分中列出了受支持的格式说明符。Supported format specifiers are listed in the Remarks section.

示例

下面的示例使用标准 "N" 格式字符串和四个不同的 CultureInfo 对象将字节值的字符串表示形式显示到控制台。The following example uses the standard "N" format string and four different CultureInfo objects to display the string representation of a byte value to the console.

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)      
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 值的格式设置为指定区域性的指定格式。The ToString(String, IFormatProvider) method formats a Byte value in a specified format of a specified culture. 若要使用当前区域性的默认("G")格式来设置数字的格式,请调用 ToString() 方法。To format a number by using the default ("G") format of the current culture, call the ToString() method. 若要使用当前区域性的指定格式设置数字的格式,请调用 ToString(String) 方法。To format a number by using a specified format of the current culture, call the ToString(String) method.

format 参数可以是标准或自定义数字格式字符串。The format parameter can be either a standard or a custom numeric format string. 除了 "R" (或 "r")以外的所有标准数字格式字符串,都支持所有自定义数字格式字符。All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. 如果 format null 或空字符串(""),则此方法的返回值将使用通用数值格式说明符("G")进行格式设置。If format is null or an empty string (""), the return value of this method is formatted with the general numeric format specifier ("G").

provider 参数是实现 IFormatProvider 接口的对象。The provider parameter is an object that implements the IFormatProvider interface. 它的 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. 实现 IFormatProvider 的对象可以是以下任一项:The object that implements IFormatProvider can be any of the following:

  • 一个表示要使用其格式设置规则的区域性的 CultureInfo 对象。A CultureInfo object that represents the culture whose formatting rules are to be used.

  • 一个包含此值的特定数字格式信息的 NumberFormatInfo 对象。A NumberFormatInfo object that contains specific numeric formatting information for this value.

  • 一个实现 IFormatProvider的自定义对象。A custom object that implements IFormatProvider.

如果 provider null 或无法从 provider获取 NumberFormatInfo 对象,则使用当前区域性的 NumberFormatInfo 对象对返回值进行格式设置。If provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value is formatted using the NumberFormatInfo object for the thread current culture. 有关线程的当前区域性的信息,请参阅 Thread.CurrentCultureFor information about the thread current culture, see Thread.CurrentCulture.

.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

另请参阅

ToString(String)

使用指定的格式将当前 Byte 对象的值转换为它的等效字符串表示形式。Converts the value of the current Byte object to its equivalent string representation using the specified format.

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

参数

format
String

一个数值格式字符串。A numeric format string.

返回

按照 Byte 参数指定的方式进行格式设置的当前 format 对象的字符串表示形式。The string representation of the current Byte object, formatted as specified by the format parameter.

异常

format 包含不受支持的说明符。format includes an unsupported specifier. “备注”部分中列出了受支持的格式说明符。Supported format specifiers are listed in the Remarks section.

示例

下面的示例使用每个受支持的标准格式字符串和一个自定义格式字符串来初始化 Byte 值并将其显示到控制台。The following example initializes a Byte value and displays it to the console using each of the supported standard format strings and a custom format string. 此示例是以 en-us 作为当前区域性运行的。The example is run with en-US as the current culture.

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           
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 参数可以是标准或自定义数字格式字符串。The format parameter can be either a standard or a custom numeric format string. 除了 "R" (或 "r")以外的所有标准数字格式字符串,都支持所有自定义数字格式字符。All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. 如果 format null 或空字符串(""),则返回值使用常规数字格式说明符("G")进行格式设置。If format is null or an empty string (""), the return value is formatted with the general numeric format specifier ("G").

此函数的返回值使用线程的当前区域性的 NumberFormatInfo 对象进行格式设置。The return value of this function is formatted using the NumberFormatInfo object for the thread current culture. 有关线程的当前区域性的信息,请参阅 Thread.CurrentCultureFor information about the thread current culture, see Thread.CurrentCulture. 若要为当前区域性以外的区域性提供格式设置信息,请调用 Byte.ToString(String, IFormatProvider) 方法。To provide formatting information for cultures other than the current culture, call the Byte.ToString(String, IFormatProvider) method.

.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

另请参阅

ToString()

将当前 Byte 对象的值转换为其等效的字符串表示形式。Converts the value of the current Byte object to its equivalent string representation.

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

返回

此对象的值的字符串表示形式,由一系列从 0 到 9 之间的数字组成,不包含前导零。The string representation of the value of this object, which consists of a sequence of digits that range from 0 to 9 with no leading zeroes.

示例

下面的示例显示字节值的数组。The following example displays an array of byte values. 请注意,此示例中未显式调用 ToString() 方法。Note that the ToString() method is not called explicitly in the example. 相反,它是隐式调用的,因为使用的是复合格式设置功能。Instead, it is called implicitly, because of the use of the composite formatting feature.

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
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 对象进行格式设置。The return value is formatted with the general numeric format specifier ("G") and the NumberFormatInfo object for the thread current culture. 若要定义 Byte 值的字符串表示形式的格式设置,请调用 ToString 方法。To define the formatting of the Byte value's string representation, call the ToString method. 若要同时定义用于创建 Byte 值的字符串表示形式的格式说明符和区域性,请调用 ToString 方法。To define both the format specifiers and culture used to create the string representation of a Byte value, call the ToString method.

.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

有关线程的当前区域性的信息,请参阅 Thread.CurrentCultureFor information about the thread current culture, see Thread.CurrentCulture.

适用于