Mono Class Library: System.Convert Overview | Members

System.Convert.ToByte Method

Converts a string representation of a number to a byte .

public static byte ToByte (string value)

Parameters

value
The string to be converted. The string is in the System.Globalization.NumberStyles.Integer style.

Returns

value as a byte.

Exceptions

TypeReason
ArgumentNullExceptionvalue is a null reference.
FormatExceptionvalue does not consist of an optional sign followed by one or more digits (zero through nine).
OverflowExceptionThe numeric value of value is greater than byte.MaxValue or less than byte.MinValue.

Remarks

Documentation for this section has not yet been entered.

Example

The following example demonstrates converting string values to byte values.

C# Example

using System;
class ConvertByteTest {
    static public void Main() {
        string string0 = "+22";
        string string1 = "0"; 
        string string2 = "-1";
        byte byte0 = Convert.ToByte(string0);
        byte byte1 = Convert.ToByte(string1);
        Console.WriteLine("(string) {0} as byte = {1}",string0,byte0);
        Console.WriteLine("(string) {0} as byte = {1}",string1,byte1);
        byte byte2 = Convert.ToByte(string2);
        Console.WriteLine("(string) {0} as byte = {1}",string2,byte2);
    }
}

The output is

(string) +22 as byte = 22
(string) 0 as byte = 0
Exception occurred: System.OverflowException: Value was either too large or too small for an unsigned byte.
at System.Byte.Parse(String s, NumberStyles style, IFormatProvider provider)
at System.Byte.Parse(String s)
at Convert.ToByte(String value)
at ConvertByteTest.Main() in C:\ECMAExamples\ConvertToByte\ConvertString.cs:line 11

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0