C#学习笔记:IP与Byte相互转换

using System;
using System.Net;

namespace IP2Byte
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个IPEndPoint
            IPEndPoint testip = new IPEndPoint(IPAddress.Parse("114.114.114.114"), 65535);
            Console.WriteLine("Input:" + testip.Address.ToString() + ":" + testip.Port);

            //转到Byte[]
            var temp = IP2Byte(testip);

            //逐个数出Byte
            Console.WriteLine("\n");
            Console.WriteLine("Byte:");
            foreach (var item in temp)
            {
                Console.Write(item + ",");
            }

            //转回IPEndPoint
            var temp2 = Byte2IP(temp);
            Console.WriteLine("\n");
            Console.WriteLine("Output:" + temp2.Address.ToString() + ":" + temp2.Port);

            //判断转换前后IPEndPoint是否相同
            Console.WriteLine("Equal:" + testip.Equals(temp2));
            Console.ReadKey();
        }

        //IP地址转Byte
        static byte[] IP2Byte(IPEndPoint ip)
        {
            byte[] output = new byte[6];
            //ip逐位转换到Byte
            for (int i = 0; i < 4; i++)
            {
                output[i] = ip.Address.GetAddressBytes()[i];
            }
            //端口号转换到Byte(16位)
            byte[] port = BitConverter.GetBytes(ip.Port);
            for (int i = 0; i < 2; i++)
            {
                output[i + 4] = port[i];
            }

            return output;
        }

        //Byte转IP地址
        static IPEndPoint Byte2IP(byte[] ip)
        {
            //前四字节逐个取int
            int[] address = new int[4];
            for (int i = 0; i < 4; i++)
            {
                address[i] = Convert.ToUInt16(ip[i]);
            }
            //后两字节取int
            int port = BitConverter.ToUInt16(ip, 4);

            IPEndPoint output = new IPEndPoint(IPAddress.Parse(string.Format("{0}.{1}.{2}.{3}",address[0], address[1], address[2], address[3])), port);
            return output;
        }
    }
}

测试:

《C#学习笔记:IP与Byte相互转换》

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注