Tuesday, August 7, 2007

C# IP utilities : Integer to dotted notation


public string IPint2dotted(long ip)
   {
     string ret = "";
     try
     {
       string hex = ip.ToString("X");

       if (hex.Length>=8)
         hex = hex.Substring(hex.Length-8);
       else
         hex = hex.PadLeft(8,'0');

       for (int i=hex.Length;i>0;i-=2)
       {
         ret += long.Parse(Convert.ToString(hex[i-2].ToString() + hex[i-1].ToString()), System.Globalization.NumberStyles.HexNumber).ToString()+".";
       }

       if (ret.Length>0)
return ret.Substring(0,ret.Length-1);
else
return "";
}
catch (Exception exc)
{
return "";
}

}

No comments: