)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
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:     /// </remarks> 
   16:     public static class Ip
   17:     {
   18:         ////////////////////////////////////////////////////////////////////////////
   19:  
   20:         /// <summary>
   21:         ///
   22:         /// </summary>
   23:         public static int IpToDec(string s)
   24:         {
   25:             return HexToDec(IpToHex(s));
   26:         }
   27:  
   28:         ////////////////////////////////////////////////////////////////////////////
   29:  
   30:         /// <summary>
   31:         ///
   32:         /// </summary>
   33:         public static string IpToHex(string ip)
   34:         {
   35:             string h;
   36:             Regex regex;
   37:             Match m;
   38:  
   39:             regex = new Regex(@"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})");
   40:  
   41:             m = regex.Match(ip);
   42:  
   43:             if (m.Success)
   44:             {
   45:                 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));
   46:             }
   47:             else h = string.Empty;
   48:  
   49:             return h;
   50:         }
   51:  
   52:         ////////////////////////////////////////////////////////////////////////////
   53:  
   54:         /// <summary>
   55:         ///
   56:         /// </summary>
   57:         public static string DecToHex(int dec)
   58:         {
   59:             uint uiDecimal = 0;
   60:             string hex;
   61:  
   62:             try
   63:             {
   64:                 // convert text string to unsigned integer
   65:                 uiDecimal = checked((uint)System.Convert.ToUInt32(dec));
   66:  
   67:                 hex = String.Format("{0:x2}", uiDecimal);
   68:             }
   69:             catch (System.OverflowException)
   70:             {
   71:                 // Show overflow message and return
   72:                 hex = string.Empty;
   73:             }
   74:  
   75:             return hex;
   76:         }
   77:  
   78:         ////////////////////////////////////////////////////////////////////////////
   79:  
   80:         /// <summary>
   81:         ///
   82:         /// </summary>
   83:         public static int HexToDec(string hex)
   84:         {
   85:             // To hold our converted unsigned integer32 value
   86:             uint dec;
   87:  
   88:             try
   89:             {
   90:                 // Convert hex string to unsigned integer
   91:                 dec = System.Convert.ToUInt32(hex, 16);
   92:             }
   93:             catch (System.OverflowException)
   94:             {
   95:                 // Show overflow message and return
   96:                 return 0;
   97:             }
   98:  
   99:             return (int)dec;
  100:         }
  101:  
  102:         ////////////////////////////////////////////////////////////////////////////
  103:  
  104:         /// <summary>
  105:         ///
  106:         /// </summary>
  107:         public static string DecToIp(int i)
  108:         {
  109:             return HexToIp(DecToHex(i));
  110:         }
  111:  
  112:         ////////////////////////////////////////////////////////////////////////////
  113:  
  114:         /// <summary>
  115:         ///
  116:         /// </summary>
  117:         public static string HexToIp(string h)
  118:         {
  119:             string s, s1, s2, s3, s4;
  120:  
  121:             h = h.PadLeft(8, '0');
  122:  
  123:             s1 = h.Substring(0, 2);
  124:             s2 = h.Substring(2, 2);
  125:             s3 = h.Substring(4, 2);
  126:             s4 = h.Substring(6, 2);
  127:  
  128:             s = HexToDec(s1) + "." + HexToDec(s2) + "." + HexToDec(s3) + "." + HexToDec(s4);
  129:  
  130:             return s;
  131:         }
  132:  
  133:         ////////////////////////////////////////////////////////////////////////////    
  134:         ////////////////////////////////////////////////////////////////////////////    
  135:     }
  136:  
  137:     ////////////////////////////////////////////////////////////////////////////
  138:     ////////////////////////////////////////////////////////////////////////////   
  139: }