0

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
itowlson
  • 73,686
  • 17
  • 161
  • 157
Stijn
  • 71
  • 1
  • 1
  • 3

2 Answers2

3

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.

itowlson
  • 73,686
  • 17
  • 161
  • 157
  • I thought it had something to do with octal values, just didn't know what exactly. Do you have any more info on that C convention? – Stijn Oct 29 '09 at 20:12
  • Good catch. javascript does this with parseInt if you're not careful. – Joel Coehoorn Oct 29 '09 at 20:14
  • Stijn: in C and C++, a literal with a leading 0 is interpreted as octal.. E.g. int i = 012 sets i to decimal 10, not decimal 12. I don't really have any further info on it (not sure what more there is to say!) but I'm sure a C tutorial or reference would provide more detail. (And "convention" was probably the wrong word... it's actually part of the language spec, not just a mere "convention" -- sorry!) – itowlson Oct 29 '09 at 20:19
1

Why are you padding it? I don't think you need the extra 0's.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • A friend sent me some code, he couldn't figure out what was going wrong. I found that he was padding it, and removing the padding got it working, but it got me wondering why exactly this didn't work. – Stijn Oct 29 '09 at 20:10