)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Ip (Ia.Ftn.Cl.Models.Business)

Public general use code classes and xml files that we've compiled and used over the years:

IP support class of Fixed Telecommunications Network (FTN) business model.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Text.RegularExpressions;
    4:  
    5: namespace Ia.Ftn.Cl.Models.Business
    6: {
    7:     ////////////////////////////////////////////////////////////////////////////
    8:  
    9:     /// <summary publish="true">
   10:     /// IP support class of Fixed Telecommunications Network (FTN) business model.
   11:     /// </summary>
   12:     /// 
   13:     /// <remarks> 
   14:     /// Copyright © 2018-2019 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   15:     ///
   16:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   17:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   18:     ///
   19:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   20:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   21:     /// 
   22:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   23:     /// 
   24:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   25:     /// </remarks> 
   26:     public static class Ip
   27:     {
   28:         ////////////////////////////////////////////////////////////////////////////
   29:  
   30:         /// <summary>
   31:         ///
   32:         /// </summary>
   33:         public static int IpToDec(string s)
   34:         {
   35:             return HexToDec(IpToHex(s));
   36:         }
   37:  
   38:         ////////////////////////////////////////////////////////////////////////////
   39:  
   40:         /// <summary>
   41:         ///
   42:         /// </summary>
   43:         public static string IpToHex(string ip)
   44:         {
   45:             string h;
   46:             Regex regex;
   47:             Match m;
   48:  
   49:             regex = new Regex(@"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})");
   50:  
   51:             m = regex.Match(ip);
   52:  
   53:             if (m.Success)
   54:             {
   55:                 h = DecToHex(int.Parse(m.Groups[1].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[2].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[3].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[4].Captures[0].Value));
   56:             }
   57:             else h = string.Empty;
   58:  
   59:             return h;
   60:         }
   61:  
   62:         ////////////////////////////////////////////////////////////////////////////
   63:  
   64:         /// <summary>
   65:         ///
   66:         /// </summary>
   67:         public static string DecToHex(int dec)
   68:         {
   69:             uint uiDecimal = 0;
   70:             string hex;
   71:  
   72:             try
   73:             {
   74:                 // convert text string to unsigned integer
   75:                 uiDecimal = checked((uint)System.Convert.ToUInt32(dec));
   76:  
   77:                 hex = String.Format("{0:x2}", uiDecimal);
   78:             }
   79:             catch (System.OverflowException)
   80:             {
   81:                 // Show overflow message and return
   82:                 hex = string.Empty;
   83:             }
   84:  
   85:             return hex;
   86:         }
   87:  
   88:         ////////////////////////////////////////////////////////////////////////////
   89:  
   90:         /// <summary>
   91:         ///
   92:         /// </summary>
   93:         public static int HexToDec(string hex)
   94:         {
   95:             // To hold our converted unsigned integer32 value
   96:             uint dec;
   97:  
   98:             try
   99:             {
  100:                 // Convert hex string to unsigned integer
  101:                 dec = System.Convert.ToUInt32(hex, 16);
  102:             }
  103:             catch (System.OverflowException)
  104:             {
  105:                 // Show overflow message and return
  106:                 return 0;
  107:             }
  108:  
  109:             return (int)dec;
  110:         }
  111:  
  112:         ////////////////////////////////////////////////////////////////////////////
  113:  
  114:         /// <summary>
  115:         ///
  116:         /// </summary>
  117:         public static string DecToIp(int i)
  118:         {
  119:             return HexToIp(DecToHex(i));
  120:         }
  121:  
  122:         ////////////////////////////////////////////////////////////////////////////
  123:  
  124:         /// <summary>
  125:         ///
  126:         /// </summary>
  127:         public static string HexToIp(string h)
  128:         {
  129:             string s, s1, s2, s3, s4;
  130:  
  131:             h = h.PadLeft(8, '0');
  132:  
  133:             s1 = h.Substring(0, 2);
  134:             s2 = h.Substring(2, 2);
  135:             s3 = h.Substring(4, 2);
  136:             s4 = h.Substring(6, 2);
  137:  
  138:             s = HexToDec(s1) + "." + HexToDec(s2) + "." + HexToDec(s3) + "." + HexToDec(s4);
  139:  
  140:             return s;
  141:         }
  142:  
  143:         ////////////////////////////////////////////////////////////////////////////    
  144:         ////////////////////////////////////////////////////////////////////////////    
  145:     }
  146:  
  147:     ////////////////////////////////////////////////////////////////////////////
  148:     ////////////////////////////////////////////////////////////////////////////   
  149: }