作者:VB.NET开发
项目:Syste
Module ByteConversion
Public Sub Main()
Dim byteStrings() As String = { Nothing, String.Empty, "1024",
"100.1", "100", "+100", "-100",
"000000000000000100", "00,100",
" 20 ", "FF", "0x1F"}
For Each byteString As String In byteStrings
CallTryParse(byteString)
Next
End Sub
Private Sub CallTryParse(stringToConvert As String)
Dim byteValue As Byte
Dim success As Boolean = Byte.TryParse(stringToConvert, byteValue)
If success Then
Console.WriteLine("Converted '{0}' to {1}", _
stringToConvert, byteValue)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.", _
stringToConvert)
End If
End Sub
End Module
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports System.Globalization
Module ByteConversion2
Public Sub Main()
Dim byteString As String
Dim styles As NumberStyles
byteString = "1024"
styles = NumberStyles.Integer
CallTryParse(byteString, styles)
byteString = "100.1"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(byteString, styles)
byteString = "100.0"
CallTryParse(byteString, styles)
byteString = "+100"
styles = NumberStyles.Integer Or NumberStyles.AllowLeadingSign _
Or NumberStyles.AllowTrailingSign
CallTryParse(byteString, styles)
byteString = "-100"
CallTryParse(byteString, styles)
byteString = "000000000000000100"
CallTryParse(byteString, styles)
byteString = "00,100"
styles = NumberStyles.Integer Or NumberStyles.AllowThousands
CallTryParse(byteString, styles)
byteString = "2E+3 "
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(byteString, styles)
byteString = "FF"
styles = NumberStyles.HexNumber
CallTryParse(byteString, styles)
byteString = "0x1F"
CallTryParse(byteString, styles)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles As NumberStyles)
Dim byteValue As Byte
Dim result As Boolean = Byte.TryParse(stringToConvert, styles, Nothing, _
byteValue)
If result Then
Console.WriteLine("Converted '{0}' to {1}", _
stringToConvert, byteValue)
Else
If stringToConvert Is Nothing Then stringToConvert = ""
Console.WriteLine("Attempted conversion of '{0}' failed.", _
stringToConvert.ToString())
End If
End Sub
End Module