0

I was working with some simple server client socket code in a Godot 3 application. For some reason, when I use this code:

IPAddress ipa = System.Net.IPAddress.Parse("131.236.053.199");

and print the result to the console via Console.WriteLine(ipa.ToString()); I get:

131.236.43.199

If I remove the leading zero from the 3rd byte and execute this line:

IPAddress ipa = System.Net.IPAddress.Parse("131.236.53.199");

and print the result via Console.WriteLine(ipa.ToString()); I get the desired address:

and print the result to the console I get:

131.236.53.199

Is there a reason why does the leading zero makes a difference? Is this a bug in the Microsoft code or am I missing something else?

I've read the documentation at https://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse(v=vs.110).aspx and couldn't see why this behaviour would occur.

Thanks.

John Forbes
  • 1,248
  • 14
  • 19
  • 1
    https://superuser.com/a/857618/52365 – GSerg Aug 07 '18 at 07:48
  • 1
    https://stackoverflow.com/a/13863545/780798 – stukselbax Aug 07 '18 at 07:49
  • Thank you for the links. Although those questions are different, the explanation there of leading zeroes signalling a number as octal rather than decimal is likely the explanation for the behaviour I observed. – John Forbes Aug 07 '18 at 07:55
  • 1
    If you check source code of the IPAddress parse method, it uses [ParseNonCanonical](https://referencesource.microsoft.com/#System/net/System/_IPv4Address.cs,bfef6214a87144d6,references) method. And there you can see i on line 184 that when 0 is detected parser switched to octal number base. So when you think you using 053 as decimal it is actually 53 as octal number. Open calculator in programmer mode and enter 53 in OCT - you will get 43 as decimal. – Renatas M. Aug 07 '18 at 08:00

0 Answers0