C# Convert ToByte(String)

Description

Convert ToByte(String) converts the specified string representation of a number to an equivalent 8-bit unsigned integer.

Syntax

Convert.ToByte(String) has the following syntax.


public static byte ToByte(
  string value
)

Parameters

Convert.ToByte(String) has the following parameters.

  • value - A string that contains the number to convert.

Returns

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

Example

The following example converts a String value to a Byte.


/*  w w w  . java2s .  co m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string stringVal = "123";
    byte byteVal = 0;
    
    try {
      byteVal = System.Convert.ToByte(stringVal);
      System.Console.WriteLine("{0} as a byte is: {1}",
        stringVal, byteVal);
    } 
    catch (System.OverflowException) {
      System.Console.WriteLine(
        "Conversion from string to byte overflowed.");
    }
    catch (System.FormatException) {
      System.Console.WriteLine(
        "The string is not formatted as a byte.");
    }
    catch (System.ArgumentNullException) {
      System.Console.WriteLine(
        "The string is null.");
    }
    
    //The conversion from byte to string is always valid.
    stringVal = System.Convert.ToString(byteVal);
    System.Console.WriteLine("{0} as a string is {1}",
      byteVal, stringVal);

  }
}
 

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