I think the real problem is that you are treating the raw form as a string; especially since it is binary(4) , you will never have to do this: just return it from db as byte[] . IpToBin great, but HexToIp should be:
public static IPAddress BinToIp(byte[] bin) { return new IPAddress(bin); }
then: the work is done. But with your existing HexToIp code HexToIp you want:
return new IPAddress(new byte[] { Convert.ToByte(ip.Substring(0,2), 16), Convert.ToByte(ip.Substring(2,2), 16), Convert.ToByte(ip.Substring(4,2), 16), Convert.ToByte(ip.Substring(6,2), 16)} ).ToString();
source share