C# Convert ToByte(Object, IFormatProvider)

Description

Convert ToByte(Object, IFormatProvider) converts the value of the specified object to an 8-bit unsigned integer, using the specified culture-specific formatting information.

Syntax

Convert.ToByte(Object, IFormatProvider) has the following syntax.


public static byte ToByte(
  Object value,
  IFormatProvider provider
)

Parameters

Convert.ToByte(Object, IFormatProvider) has the following parameters.

  • value - An object that implements the IConvertible interface.
  • provider - An object that supplies culture-specific formatting information.

Returns

Convert.ToByte(Object, IFormatProvider) method returns An 8-bit unsigned integer that is equivalent to value, or zero if value is null.

Example


using System;//  w  w w .ja  va2 s  . co m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberFormatInfo provider = new NumberFormatInfo();
      provider.PositiveSign = "pos ";
      provider.NegativeSign = "neg ";
      provider.NumberDecimalSeparator = ".";

      object[] numericStrings = { "234", "+234", "pos 234", "234.", "255", 
                                  "256", "-1" };

      foreach (string numericString in numericStrings)
      {
         Console.Write("'{0,-8}' ->   ", numericString);
         try {
            byte number = Convert.ToByte(numericString, provider);
            Console.WriteLine(number);
         }   
         catch (FormatException) {
            Console.WriteLine("Incorrect Format");
         }                             
         catch (OverflowException) {
            Console.WriteLine("Overflows a Byte");
         }   
      }
   }
}

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