为什么 IPAddress.Parse(“192.168.001.001") 有效而 IPAddress.Parse(“192.168.001.009") 无效? [英] Why IPAddress.Parse("192.168.001.001") works while IPAddress.Parse("192.168.001.009") don't?

查看:36
本文介绍了为什么 IPAddress.Parse(“192.168.001.001") 有效而 IPAddress.Parse(“192.168.001.009") 无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从 API 结果解析 IP 地址,其中 IPv4 地址的四个部分中的每一个都以 0(零)为前缀.像这样:

I'm stuck trying to parse IP addresses from a API result where each of the four pats of the IPv4 Address comes prefixed with 0 (zeroes). Something like this:

127.000.000.001 而不是 127.0.0.1

127.000.000.001 instead of 127.0.0.1

我在尝试解析 192.168.001.009 时开始遇到解析错误.它也无法用于 192.168.001.008,但适用于 007、006、005 到 001!!!

I started getting parse errors when trying to parse 192.168.001.009. It also fails for 192.168.001.008, but works for 007, 006, 005 up to 001!!!

它也无法用于 192.168.001.018,但适用于 .017、0.016 到 010!

It also fails for 192.168.001.018, but works for .017, .016 down to 010!

它适用于 192.168.001.8 或 .8 以及 192.168.001.18 和 .19...

It works for 192.168.001.8 or .8 and also 192.168.001.18 and .19...

这是 CLR 中的错​​误吗?还是我错过了一些愚蠢的东西?

Is this a bug in the CLR? Or am I missing something stupid?

试试吧:

IPAddress.Parse("192.168.001.007"); // works
IPAddress.Parse("192.168.001.87"); // works
IPAddress.Parse("192.168.001.008"); // throws exception
IPAddress.Parse("192.168.001.19"); // works
IPAddress.Parse("192.168.001.019");  // throws exception
// and so on!

推荐答案

由于数字从 0 开始,因此被解释为八进制而不是十进制.这些不是 C# 文字,因此由库来以一种或另一种方式解释它.

The numbers, since they are starting with 0, are being interpreted as octal instead of decimal. These are not C# literals, so it's up to the library to interpret it one way or another.

测试它的一个简单方法是构造一个以.010"结尾的 IP,解析它,你会看到它被解析为一个以 .8 结尾的 IP.

A simple way to test it would be to construct an IP ending in ".010", parse it, and you'll see that it was parsed as an ip ending in .8.

一种可能的快速而肮脏的解决方案是搜索正则表达式 /.0*/ 并将其替换为."

A possible quick and dirty solution would be to search for the regex /.0*/ and replace it with "."

您可以在维基百科条目中找到有关 Dot-decimal-notation 的更多信息:

You can find more information on the wikipedia entry for Dot-decimal-notation:

一个流行的 IP 网络实现,起源于 4.2BSD,包含一个函数 inet_aton() 用于将字符串表示的 IP 地址转换为内部二进制存储.除了基本的四位十进制格式和完整的 32 位地址外,它还支持 octet.24bits(例如 10.1234567;对于 A 类地址)和 octet.octet.16bits(例如 172.16.12345;对于 B 类地址)的中间语法).它还允许数字以十六进制和八进制写入,分别在它们前面加上 0x 和 0.直到今天,软件仍继续支持这些功能,即使它们被视为非标准.但这也意味着 IP 地址组件以前导零数字写入的地址可能会被不同的程序不同地解释:有些会忽略前导零,有些会将数字解释为八进制.

A popular implementation of IP networking, originating in 4.2BSD, contains a function inet_aton() for converting IP addresses in character strings representation to internal binary storage. In addition to the basic four-decimals format and full 32-bit addresses, it also supported intermediate syntaxes of octet.24bits (e.g. 10.1234567; for Class A addresses) and octet.octet.16bits (e.g. 172.16.12345; for Class B addresses). It also allowed the numbers to be written in hexadecimal and octal, by prefixing them with 0x and 0, respectively. These features continue to be supported by software until today, even though they are seen as non-standard. But this also means addresses where an IP address component is written with a leading zero digit may be interpreted differently by different programs: some will ignore the leading zero, some will interpret the number as octal.

这篇关于为什么 IPAddress.Parse(“192.168.001.001") 有效而 IPAddress.Parse(“192.168.001.009") 无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆