Convert.ToByte Method (String)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts the specified String representation of a number to an equivalent 8-bit unsigned integer.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.String
A String containing a number to convert.
Return Value
Type: System.ByteAn 8-bit unsigned integer equivalent to the value of value.
-or-
Zero if value is Nothing.
| Exception | Condition |
|---|---|
| FormatException | value does not consist of an optional sign followed by a sequence of digits (zero through nine). |
| OverflowException | value represents a number less than MinValue or greater than MaxValue. |
If you prefer not to handle an exception if the conversion fails, you can call the Byte.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.
The following code sample illustrates the use of ToByte, converting a String value to a Byte :
Public Sub ConvertStringByte(ByVal stringVal As String) Dim byteVal As Byte = 0 Try byteVal = System.Convert.ToByte(stringVal) outputBlock.Text &= String.Format("{0} as a byte is: {1}", _ stringVal, byteVal) & vbCrLf Catch exception As System.OverflowException outputBlock.Text &= String.Format( _ "Overflow in string-to-byte conversion.") & vbCrLf Catch exception As System.FormatException outputBlock.Text &= String.Format( _ "The String is not formatted as a Byte.") & vbCrLf Catch exception As System.ArgumentException outputBlock.Text &= "The String is null." & vbCrLf End Try 'The conversion from byte to string is always valid. stringVal = System.Convert.ToString(byteVal) outputBlock.Text &= String.Format("{0} as a string is {1}", _ byteVal, stringVal) & vbCrLf End Sub