This method is used to convert the value of the current Byte object to its equivalent string representation. There are total 4 methods in the overload list of Byte.ToString() Method as follows:
- ToString(IFormatProvider)
- ToString(String, IFormatProvider)
- ToString()
- ToString(String)
ToString(IFormatProvider)
This method is used to Converts the numeric value of the current Byte object to its equivalent string representation using the specified culture-specific formatting information.
Syntax:
public string ToString (IFormatProvider provider);
Parameters: This method takes an object of type IFormatProvider that supplies culture-specific formatting information.
Return Value: This method returns the string representation of the value of this object in the format specified by the provider parameter.
Below programs illustrate the use of Byte.ToString(IFormatProvider) Method:
Example 1:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
byte byteValue = 15;
CultureInfo provider = new CultureInfo( "en-us" );
string value = byteValue.ToString(provider);
Console.WriteLine( "value is {0} and provider is {1}" ,
value, provider.Name);
}
}
|
Output:
value is 15 and provider is en-US
Byte.ToString(String, IFormatProvider) Method
This method is used to convert the value of the current Byte object to its equivalent string representation using the specified format and culture-specific formatting information.
Syntax:
public string ToString (string format, IFormatProvider provider);
Parameters:
format: It is a standard or custom numeric format string.
provider: It is an object that supplies culture-specific formatting information.
Return Value: This method returns the string representation of the value of this object in the format specified by the provider parameter.
Exceptions: This method will give FormatException if the format includes an unsupported specifier.
Below programs illustrate the use of Byte.ToString(String, IFormatProvider) Method:
Example 1:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
byte byteValue = 15;
CultureInfo provider = new CultureInfo( "fr-FR" );
string format = "D5" ;
string value = byteValue.ToString(format, provider);
Console.WriteLine( "value is {0} and provider is {1}" ,
value, provider.Name);
}
}
|
Output:
value is 00015 and provider is fr-FR
Example 2: For FormatException
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
byte byteValue = 15;
CultureInfo provider = new CultureInfo( "fr-FR" );
string format = "s5" ;
Console.WriteLine( "format is invalid" );
string value = byteValue.ToString(format, provider);
Console.WriteLine( "value is {0} and provider is {1}" ,
value, provider.Name);
}
catch (FormatException e) {
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
format is invalid
Exception Thrown: System.FormatException
Byte.ToString() Method
This method is used to convert the value of the current Byte object to its equivalent string representation.
Syntax:
public override string ToString ();
Return Value: This method 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.
Below programs illustrate the use of Byte.ToString() Method:
Example 1:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
byte byteValue = 15;
string value = byteValue.ToString();
Console.WriteLine( "value is {0} " , value);
}
}
|
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Dec, 2019
Like Article
Save Article