Код:
#region GetAddressBytes
/// <summary>
/// Provides a copy of the IPAddress as an array of bytes.
/// </summary>
/// <param name="ipAddress">The IPAddress.</param>
/// <returns>Array of bytes.</returns>
public static byte[] GetAddressBytes(IPAddress ipAddress)
{
return GetAddressBytes(ipAddress.ToString());
}
/// <summary>
/// Provides a copy of the IPAddress as an array of bytes.
/// </summary>
/// <param name="ipAddress">A dotted IP address string.</param>
/// <returns>Array of bytes.</returns>
public static byte[] GetAddressBytes(string ipAddress)
{
byte[] bytes = new byte[4];
try
{
string[] sBytes = ipAddress.Split('.');
for (int i = 0; i < 4; i++)
bytes[i] = byte.Parse(sBytes[i]);
}
catch
{
throw new Exception(string.Format("Could not parse IP address string '{0}'.", ipAddress));
}
return bytes;
}
private static byte[] GetAddressBytes(long longAddress)
{
byte[] bytes = new Byte[4];
double temp = longAddress / 16777216d;
double tempMod = longAddress % 16777216d;
bytes[0] = (byte)Math.Floor(temp);
temp = tempMod / 65536d;
tempMod = tempMod % 65536d;
bytes[1] = (byte)Math.Floor(temp);
temp = tempMod / 256d;
tempMod = tempMod % 256d;
bytes[2] = (byte)Math.Floor(temp);
bytes[3] = (byte)(tempMod);
return bytes;
}
#endregion