.NET My.Computer.Network.Ping 和左键盘
以下代码片段在用 0 填充最后一部分时会抛出错误,最后一部分是 008、009、018、019、028、029 等。 有人知道为什么吗?
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.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
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.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测前导零会导致某些子系统将数字解释为八进制(旧的 C 约定)。 8 和 9 是无效的八进制数字,因此包含 8 和 9 的八进制值会导致错误。
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.
你为什么要垫它?我认为你不需要额外的 0。
Why are you padding it? I don't think you need the extra 0's.