Edit

Share via


Byte.ToString Method

Definition

Converts the value of the current Byte object to its equivalent string representation.

Overloads

ToString(IFormatProvider)

Converts the numeric value of the current Byte object to its equivalent string representation using the specified culture-specific formatting information.

ToString(String)

Converts the value of the current Byte object to its equivalent string representation using the specified format.

ToString(String, IFormatProvider)

Converts the value of the current Byte object to its equivalent string representation using the specified format and culture-specific formatting information.

ToString()

Converts the value of the current Byte object to its equivalent string representation.

ToString(IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Converts the numeric value of the current Byte object to its equivalent string representation using the specified culture-specific formatting information.

C#
public string ToString(IFormatProvider provider);
C#
public string ToString(IFormatProvider? provider);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

The string representation of the value of this object in the format specified by the provider parameter.

Implements

Examples

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.

C#
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)

Remarks

The return value is formatted with the general numeric format specifier ("G").

The provider parameter is an object that implements the IFormatProvider interface. Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. The object that implements IFormatProvider can be any of the following:

  • A CultureInfo object that represents the culture whose formatting rules are to be used.

  • A NumberFormatInfo object that contains specific numeric formatting information for this value.

  • A custom object that implements IFormatProvider.

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. For information about the thread current culture, see Thread.CurrentCulture.

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Converts the value of the current Byte object to its equivalent string representation using the specified format.

C#
public string ToString(string format);
C#
public string ToString(string? format);

Parameters

format
String

A numeric format string.

Returns

The string representation of the current Byte object, formatted as specified by the format parameter.

Exceptions

format includes an unsupported specifier. Supported format specifiers are listed in the Remarks section.

Examples

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. The example is run with en-US as the current culture.

C#
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

Remarks

The format parameter can be either a standard or a custom numeric format string. All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. If format is null or an empty string (""), the return value is formatted with the general numeric format specifier ("G").

The return value of this function is formatted using the NumberFormatInfo object for the thread current culture. For information about the thread current culture, see Thread.CurrentCulture. To provide formatting information for cultures other than the current culture, call the Byte.ToString(String, IFormatProvider) method.

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Converts the value of the current Byte object to its equivalent string representation using the specified format and culture-specific formatting information.

C#
public string ToString(string format, IFormatProvider provider);
C#
public string ToString(string? format, IFormatProvider? provider);

Parameters

format
String

A standard or custom numeric format string.

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

The string representation of the current Byte object, formatted as specified by the format and provider parameters.

Implements

Exceptions

format includes an unsupported specifier. Supported format specifiers are listed in the Remarks section.

Examples

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.

C#
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)

Remarks

The ToString(String, IFormatProvider) method formats a Byte value in a specified format of a specified culture. To format a number by using the default ("G") format of the current culture, call the ToString() method. To format a number by using a specified format of the current culture, call the ToString(String) method.

The format parameter can be either a standard or a custom numeric format string. All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. If format is null or an empty string (""), the return value of this method is formatted with the general numeric format specifier ("G").

The provider parameter is an object that implements the IFormatProvider interface. Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. The object that implements IFormatProvider can be any of the following:

  • A CultureInfo object that represents the culture whose formatting rules are to be used.

  • A NumberFormatInfo object that contains specific numeric formatting information for this value.

  • A custom object that implements IFormatProvider.

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. For information about the thread current culture, see Thread.CurrentCulture.

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString()

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Converts the value of the current Byte object to its equivalent string representation.

C#
public override string ToString();

Returns

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.

Examples

The following example displays an array of byte values. 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, the F# example uses string interpolation.

C#
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

Remarks

The return value is formatted with the general numeric format specifier ("G") and the NumberFormatInfo object for the thread current culture. To define the formatting of the Byte value's string representation, call the ToString method. To define both the format specifiers and culture used to create the string representation of a Byte value, call the ToString method.

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

For information about the thread current culture, see Thread.CurrentCulture.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0