Mega Code Archive

 
Categories / C# / Network
 

IP to Uint

using System; using System.Net; using System.IO; using System.Windows.Forms; namespace PeeIRC {     class Utils     {         public static uint IpToUint(IPAddress ip)         {             uint uip = (uint)ip.Address; //TODO try use ip.GetAddressBytes instead             //fix endianess from network             if (BitConverter.IsLittleEndian)             {                 byte[] bytes = BitConverter.GetBytes(uip);                 uip = (uint)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]);             }             return uip;         }         public static IPAddress UintToIP(uint ip)         {             //fix endianess from network             if (BitConverter.IsLittleEndian)             {                 ip = ((ip << 24) & 0xFF000000) + ((ip << 8) & 0x00FF0000) + ((ip >> 8) & 0x0000FF00) + ((ip >> 24) & 0x000000FF);             }             return new System.Net.IPAddress(ip);         }     } }