C# Byte Parse(String)

Description

Byte Parse(String) converts the string representation of a number to its Byte equivalent.

Syntax

Byte.Parse(String) has the following syntax.


public static byte Parse(
  string s
)

Parameters

Byte.Parse(String) has the following parameters.

  • s - A string that contains a number to convert. The string is interpreted using the Integer style.

Returns

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

Example

The following example demonstrates how to convert a string value into a byte value using the Byte.Parse(String) method.


using System;/*from   w w w  .j a v a 2 s  .c om*/

public class MainClass {
  public static void Main(String[] argv){  
    string stringToConvert = " 162";
    byte byteValue;
    try
    {
       byteValue = Byte.Parse(stringToConvert);
       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