topic
1. Question
01---IPv4地址转换成整数
2. Ideas for interpreting pictures
1. Convert IP address to binary
2. Convert binary to decimal
3. Things to note
1. IP address range judgment
2. Empty string judgment
3. Illegal character judgment
4. Test points
1. Related usages of string split, convert, etc.
2. Regular expressions
3. Base conversion
4. Understanding the meaning of 32-bit integers
5. Code
I haven’t written the judgment code. I feel like I can just judge the input strings one by one.
string[] ipv4Node = Ipv4.Split('#');
string str = null;
//挨个判断可能情况
if (Ipv4.Length < 7 || Ipv4.Length > 15)
{
Console.WriteLine("invalid IP");
return;
}
else if (string.IsNullOrWhiteSpace(Ipv4))
{
Console.WriteLine("invalid IP");
return;
}
for (int i = 0; i < ipv4Node.Length; i++)
{
//将IP地址转为二进制
str+= Convert.ToString(Convert.ToInt16( ipv4Node[i]?.ToString(), 16), 2).PadLeft(8, '0');
}
//二进制转十进制
var res=Convert.ToInt32(str,2);
Console.WriteLine( res );
Console.ReadKey();