.NET My.Computer.Network.Ping & PadLeft
Following code snippet throws an error when padding the last part with 0, and the last part is 008, 009, 018, 019, 028, 029 etc. Anyone got an idea why?
Sub Main()
Dim fixed As String = "192.168.0."
Dim ip1, ip2 As String
For i As Int32 = 1 To 255
ip1 = fixed & Convert.ToString(i)
Console.Write(ip1 & " - ")
Try
Console.开发者_StackOverflow社区WriteLine(My.Computer.Network.Ping(ip1))
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
ip2 = fixed & Convert.ToString(i).PadLeft(3, "0"c)
Console.Write(ip2 & " - ")
Try
Console.WriteLine(My.Computer.Network.Ping(ip2))
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Next
End Sub
I'd guess that the leading zero causes some subsystem to interpret the number as octal (an old C convention). 8 and 9 are invalid octal digits, so octal values with 8 and 9 in them would cause an error.
Why are you padding it? I don't think you need the extra 0's.
精彩评论