Mega Code Archive

 
Categories / VB.Net / Development
 

Convert ToByte (String) converts string to 8-bit unsigned integer

Class Sample    Public Shared Sub Main()     Dim stringVal As String = "123"     Dim byteVal As Byte = 0     Try         byteVal = System.Convert.ToByte(stringVal)         System.Console.WriteLine("{0} as a byte is: {1}", _                                   stringVal, byteVal)     Catch exception As System.OverflowException         System.Console.WriteLine( _             "Overflow in string-to-byte conversion.")     Catch exception As System.FormatException         System.Console.WriteLine( _             "The String is not formatted as a Byte.")     Catch exception As System.ArgumentException         System.Console.WriteLine("The String is null.")     End Try     '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)     End Sub End Class