C# Byte Parse(String, IFormatProvider)

Description

Byte Parse(String, IFormatProvider) converts the string representation of a number in a specified culture-specific format to its Byte equivalent.

Syntax

Byte.Parse(String, IFormatProvider) has the following syntax.


public static byte Parse(
  string s,
  IFormatProvider provider
)

Parameters

Byte.Parse(String, IFormatProvider) has the following parameters.

  • s - A string that contains a number to convert. The string is interpreted using the Integer style.
  • provider - An object that supplies culture-specific parsing information about s. If provider is null, the thread current culture is used.

Returns

Byte.Parse(String, IFormatProvider) method returns A byte value that is equivalent to the number contained in s.

Example

The following example parses string representations of Byte values with the Parse method.


/*from   w  ww.  j a  v a2s . c o m*/
using System;
using System.Globalization;

public class MainClass {
  public static void Main(String[] argv){ 
        string stringToConvert; 
        byte byteValue;
        
        stringToConvert = " 214 ";
        try {
           byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
           Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
        }
        catch (FormatException) {
           Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
        catch (OverflowException) {
           Console.WriteLine("'{0}' is greater than {1} or less than {2}.", 
                             stringToConvert, Byte.MaxValue, Byte.MinValue); 
        }

        stringToConvert = " + 214 ";
        try {
           byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
           Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
        }
        catch (FormatException) {
           Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
        catch (OverflowException) {
           Console.WriteLine("'{0}' is greater than {1} or less than {2}.", 
                             stringToConvert, Byte.MaxValue, Byte.MinValue); 
        }
        
        stringToConvert = " +214 ";
        try {
           byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
           Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
        }
        catch (FormatException) {
           Console.WriteLine("Unable to parse '{0}'.", stringToConvert); 
        }
        catch (OverflowException) {
           Console.WriteLine("'{0}' is greater than {1} or less than {2}.", 
                             stringToConvert, Byte.MaxValue, Byte.MinValue); 
        }
 
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version