Converts a string representation of a number to a byte .
- value
- The string to be converted. The string is in the System.Globalization.NumberStyles.Integer style.
value as a byte.
Type Reason ArgumentNullException value is a null reference. FormatException value does not consist of an optional sign followed by one or more digits (zero through nine). OverflowException The numeric value of value is greater than byte.MaxValue or less than byte.MinValue.
Documentation for this section has not yet been entered.
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
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0