System.Net.IPAddress.GetAddressBytes C# (CSharp) Method

GetAddressBytes() public method

public GetAddressBytes ( ) : byte[]
return byte[]
        public byte[] GetAddressBytes()
        {
            byte[] bytes;
            if (IsIPv6)
            {
                bytes = new byte[NumberOfLabels * 2];

                int j = 0;
                for (int i = 0; i < NumberOfLabels; i++)
                {
                    bytes[j++] = (byte)((_numbers[i] >> 8) & 0xFF);
                    bytes[j++] = (byte)((_numbers[i]) & 0xFF);
                }
            }
            else
            {
                uint address = PrivateAddress;
                bytes = new byte[IPAddressParserStatics.IPv4AddressBytes];
                bytes[0] = (byte)(address);
                bytes[1] = (byte)(address >> 8);
                bytes[2] = (byte)(address >> 16);
                bytes[3] = (byte)(address >> 24);
            }
            return bytes;
        }

Usage Example

Esempio n. 1
1
        public static string GetNetworkAddressAsString(IPAddress address, IPAddress mask)
        {
            string netIP = ""; // Network address as string
            int networkLength = 0;

            if (null != mask)
            {
                for (int i = 0; i < 4; i++)
                {
                    byte ba = address.GetAddressBytes()[i];
                    byte bm = mask.GetAddressBytes()[i];

                    netIP += ba & bm;
                    if (i < 3) netIP += ".";
                    networkLength += 8 - (int)System.Math.Truncate(System.Math.Log(256 - bm, 2));
                }
                netIP += "/" + networkLength;
            }
            else
            {
                netIP = address.ToString() + "/32";
            }

            return netIP;
        }
All Usage Examples Of System.Net.IPAddress::GetAddressBytes