Mega Code Archive

 
Categories / VB.Net / Development
 

Convert string by different base

Module Example    Public Sub Main()       Dim bases() As Integer = { 2, 8, 10, 16 }       Dim values() As String = { "-1", "1", "08", "0F", "11" , "12", "30", _                                                   "101", "255", "FF", "10000000", "80" }       Dim number As Byte       For Each base As Integer In bases          Console.WriteLine("Base {0}:", base)          For Each value As String In values             Try                number = Convert.ToByte(value, base)                Console.WriteLine("   Converted '{0}' to {1}.", value, number)             Catch e As FormatException                Console.WriteLine("   '{0}' is not in the correct format for a base {1} conversion.", _                                  value, base)             Catch e As OverflowException                Console.WriteLine("   '{0}' is outside the range of the Byte type.", value)             Catch e As ArgumentException                Console.WriteLine("   '{0}' is invalid in base {1}.", value, base)             End Try             Next                                        Next     End Sub End Module