jsakamoto / ipaddressrange

.NET Class Library for range of IP address, both IPv4 and IPv6.
Mozilla Public License 2.0
368 stars 71 forks source link

Wrong Ip tryparse #73

Closed MartynenkoA closed 2 years ago

MartynenkoA commented 2 years ago

Is it normal that ip 0037.15.0.1 parsing to 31.15.0.1 17.15.011 parsing to 17.15.0.9

should there be an exception?

jsakamoto commented 2 years ago

Interestingly, those parsing results are valid and correct at all!

Actually, if the number has a "0" prefix, it is parsed as an octal format number.

You can see octal "37" represents "31" in decimal number format in the Windows calculator app in "Programmer" mode.

image

Another example, you can execute the ping command with the argument "0037.15.0.1", and of course, It will work correctly.

image

In fact, IPv4 address formatting is very funny. All of the following examples represent valid and correct IPv4 addresses!

See also: https://nxmnpg.lemoda.net/3/inet_aton#5

Happy coding! ;)