试着做了下,不知道对不对,请指教!!- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ip转换为2进制
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("输入IP:");
- string ip = Console.ReadLine();
- int sub;//"."的节点,用以IP地址分段处理
- sub = 0;
- try
- {
- for (int i = sub; i < ip.Length; i++)//读取出每一段IP地址
- {
- string ipduan;
- ipduan = "";
- string ip1 = ip.Substring(i, 1);
- if (ip1 == ".")
- {
- ipduan = ip.Substring(sub, i - sub);
- Console.Write(Convert.ToString(Convert.ToInt32(ipduan), 2));
- Console.Write(".");
- sub = i + 1;
- }
- }
- }
- catch
- {
- Console.WriteLine("输入有误!");
- }
- try
- {
- string p2 = ip.Substring(sub, ip.Length - sub);//最后一个“.”后面数字的处理
- Console.Write(Convert.ToString(Convert.ToInt32(p2), 2));
- }
- catch
- {
- Console.WriteLine("输入有误!");
- }
- Console.ReadKey();
- }
- }
- }
- {:soso_e100:}
复制代码 |