IP和前缀替换数据

问题描述 投票:0回答:0

大家好,我有 C# 代码,但我不知道如何将我的数据替换到代码中。 示例:我有 IP

113.31.197.210
和两个前缀
8
10
我需要替换的位置和内容

我试过ChatGPT,但没用。

代码

using System;
using System.ComponentModel;

namespace IPCalcDemo
{
    public static class IPCalc
    { 
        //краще використовувати якщо ООП стайл
        public const UInt32 classBMask = (UInt32)0b1000<<28;
        public const UInt32 classCMask = (UInt32)0b1100<<28;
        public const UInt32 classDMask = (UInt32)0b1110<<28;
        public const UInt32 classEMask = (UInt32)0b1111<<28;
            
        public static UInt32 CreateMaskFromPrefix(uint preffix)
        {
            UInt32 NetMask=0;
            for (uint i = 1; i <= preffix; ++i)
            {
                NetMask <<= 1;
                NetMask += 1;
            }
            NetMask=NetMask<<(int)(32-preffix);
            return NetMask;
        }

        public static string ConvertIPToDecString(UInt32 ip)
        {
            string[] a = new string[4];
            for (int i = 0; i < 4; ++i) 
            {
                a[3 - i] = (ip % 256).ToString();
                ip /= 256;
            }
            return string.Join(".", a);
        }
        public static string ConvertIPToBinString(UInt32 ip)
        {
            string res = "";
            UInt32 key = ((UInt32)1) << 31,val=0;
            for (int i = 1; i <= 32; ++i)
            {
                val=key&ip;
                res+=val!=0?"1":"0";
                ip<<=1;
                if(0==i%8 && i!=32)
                    res+=".";
            }
            return res;
        }

        public static UInt32 ConvertStringToIP(string str)
        {
            UInt32 ResolvedIP=0;
            string[] Dlist = str.Split('.');
            for (int i = 0; i < 4; ++i) 
                ResolvedIP += UInt32.Parse(Dlist[i]) << (8 * (3 - i));
            return ResolvedIP;
        }

        public static UInt32 GetNetworkAddresFromIP(UInt32 ip,UInt32 netMask)
        {
            return ip&netMask;
        }

        public static UInt32 GetFirstHostAddress(UInt32 networkIp)
        {
            return networkIp+1;
        }

        public static UInt32 GetBroadcastAddress(UInt32 networkIP, UInt32 netMask)
        {
            return (networkIP & netMask) | (~netMask);
        }

        public static UInt32 GetLastHostAddress(UInt32 networkIP, UInt32 netMask)
        {
            return networkIP & netMask | ((~netMask)-1);
        }

        public static string GetNetworkClass(UInt32 ip)
        {
            if ((ip & classEMask) == classEMask)
                return "E";
            if ((ip & classDMask) == classDMask)
                return "D";
            if ((ip & classCMask) == classCMask)
                return "C";
            if ((ip & classBMask) == classBMask)
                return "B";
            return "A";
        }
    }

    public static class IPHelper
    {
        public static void GetIPBriefInfo(UInt32 ip, uint prefix)
        {
            Console.WriteLine("====================< IP >==================");
            Console.WriteLine($"str IP: {IPCalc.ConvertIPToDecString(ip)}");
            Console.WriteLine($"bin IP: {IPCalc.ConvertIPToBinString(ip)}");
            Console.WriteLine($"int IP: {ip}");
            Console.WriteLine($"Detected class: {IPCalc.GetNetworkClass(ip)}");
            Console.WriteLine($"Resolved  IP:{IPCalc.ConvertIPToDecString(ip)}");
            uint netMask = IPCalc.CreateMaskFromPrefix(prefix);
            Console.WriteLine($"Network Mask:{IPCalc.ConvertIPToDecString(netMask)}");
            uint netIP=IPCalc.GetNetworkAddresFromIP(ip,netMask);
            Console.WriteLine("Network   IP:{0}",IPCalc.ConvertIPToDecString(netIP));
            Console.WriteLine("Broadcast IP:{0}",IPCalc.ConvertIPToDecString(IPCalc.GetBroadcastAddress(netIP,netMask)));
            Console.WriteLine("First HostIP:{0}",IPCalc.ConvertIPToDecString(IPCalc.GetFirstHostAddress(netIP)));
            Console.WriteLine("Last Host IP:{0}",IPCalc.ConvertIPToDecString(IPCalc.GetLastHostAddress(netIP,netMask)));
        }
        
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            string[] rawip = new[] {"10.0.0.1","10.0.0.2","192.168.0.0","11.22.33.240","131.122.224.230" };
            uint[] prefix = new[] { 8U, 17U, 24U, 10U, 14U };
            UInt32[] ipList=Array.ConvertAll(rawip,IPCalc.ConvertStringToIP);
            for(int i=0; i<prefix.Length;++i)
            {
                 IPHelper.GetIPBriefInfo(ipList[i], prefix[i]);
            }
        }
        
    }
}
c# networking ip
© www.soinside.com 2019 - 2024. All rights reserved.