IPv4 Functions

32-bit Unsigned Integer to IPv4 Components

  1. byte   ipv4Addr_octet1(uint uintIpAddress) – Isolate the 1st octet of a 32-bit Integer
  2. byte   ipv4Addr_octet2(uint uintIpAddress) – Isolate the 2nd octet of a 32-bit Integer
  3. byte   ipv4Addr_octet3(uint uintIpAddress) – Isolate the 3rd octet of a 32-bit Integer
  4. byte   ipv4Addr_octet4(uint uintIpAddress) – Isolate the 4th octet of a 32-bit Integer
  5. string    uintToString(uint uintIpAddress) – Convert 32-bit Integer to Dot-Notation

Network Manipulation Functions

  1. uint ipv4_network(uint uintIpAddress, uint uintMaskLength) – Finds network based on address and Mask

IPv4 Components to 32-bit Unsigned Integer:

Note using Excel string functions to isolate octets: LEFT(string,length) MID(string,start,length) LEN(string)

ipv4Octet_Addr(char* strIpAddress) =

  (
    MIN(
      MAX(
        INT(
          MID( strIpAddress,
            FIND(".",
              strIpAddress,
                FIND(".",
                  strIpAddress,
                  FIND(".", strIpAddress ) + 1
                ) + 1
              ) + 1,
            3
          )
        ),
        0
      ),
      255
      
    ) + (
    
      MIN(
        MAX(INT(
        MID( strIpAddress,
          FIND(".",
            strIpAddress,
            FIND(".", strIpAddress ) + 1
          ) + 1,
          FIND(".",
            strIpAddress,
            FIND(".",
              strIpAddress,
              FIND(".", strIpAddress ) + 1
            ) + 1
          ) -
          (
            FIND(".",
              strIpAddress,
              FIND(".", strIpAddress ) + 1
            ) + 1
          )
        )
        ),0)
      ,255
      ) * 256 
      
    ) + (
    
      MIN(
        MAX(
          INT(
            MID( 
              strIpAddress,
              FIND(".", strIpAddress ) + 1,
              FIND(".",
                strIpAddress,
                FIND(".", strIpAddress ) + 1
              ) - (FIND(".", strIpAddress ) + 1)
            )
          ),
          0
        ),
        255
      ) * 65535
      
    ) + (
    
      MIN(
        MAX(
          INT(
            LEFT( strIpAddress, FIND(".", strIpAddress ))
          ),
          0
        ),
        255
      ) * 2^24
    )
  )

Where:

  strIpAddress = 10.73.64.128

result: 172572800

32-bit Unsigned Integer to IPv4 Components: (Octet1.Octet2.Octet3.Octet4)

Note that (^) represents raising the left numeral to the right power and INT(fn(..)) is being used to round off the decimal.

Isolate the 1st octet of a 32-bit Integer:

ipv4Addr_octet1(uint uintIpAddress) =

  INT(uintIpAddress / 16777216)

Where:

  uintIpAddress = 172572800
  
result: 10

Isolate the 2nd octet of a 32-bit Integer:

ipv4Addr_octet2(uint uintIpAddress) =

  INT( (uintIpAddress - INT(uintIpAddress / 16777216)*16777216)/65536 )

Where:

  uintIpAddress = 172572800

result: 73

Isolate the 3rd octet of a 32-bit Integer:

ipv4Addr_octet3(uint uintIpAddress) =

  INT( 
    (
      uintIpAddress - 
      INT(uintIpAddress/16777216) * 16777216 - 
      INT( 
        ( 
          uintIpAddress - 
          INT(uintIpAddress/16777216)*16777216 
        ) / 65536 
      ) * 65536 
    ) / 256 
  )

Where:

  uintIpAddress = 172572800

result: 64

Isolate the 4th octet of a 32-bit Integer:

ipv4Addr_octet4(uint uintIpAddress) =

  (
    uintIpAddress - 
    INT(uintIpAddress/16777216)*16777216 - 
    INT( 
      ( 
        uintIpAddress - 
        INT(uintIpAddress/16777216)*16777216 
      ) / 65536 
    ) * 65536 - INT( 
      (
        H:H - 
        INT(uintIpAddress/16777216)*16777216 - 
        INT( 
          (
            uintIpAddress - 
            INT(uintIpAddress/16777216)*16777216 
          )/65536 
        ) * 65536 
      ) / 256 
    ) * 256 
  )

Where:

  uintIpAddress = 172572800

result: 128

IP Address in Dot-Notation

Combines the previous Octet Isolation functions into a single function resulting in a String Dot-Notation

uintToString(uint uintIpAddress) =

  INT(uintIpAddress / 16777216) & "." & 
  
  INT( 
    (
      uintIpAddress - 
      INT(uintIpAddress / 16777216)*16777216
    ) / 65536 
  ) & "." & 
  
  INT( 
    (
      uintIpAddress - 
      INT(uintIpAddress/16777216)*16777216 - 
      INT( 
        (
          uintIpAddress - 
          INT(uintIpAddress/16777216)*16777216 
        ) / 65536 
      ) * 65536 
    ) / 256 
  ) & "." & 
  
  (
    uintIpAddress - 
    INT(uintIpAddress/16777216)*16777216 - 
    INT( 
      (
        uintIpAddress - 
        INT(uintIpAddress/16777216)*16777216 
      ) / 65536 
    ) * 65536 - 
    INT( 
      (
        uintIpAddress - 
        INT(uintIpAddress/16777216)*16777216 - 
        INT( 
          (
            uintIpAddress - 
            INT(uintIpAddress/16777216)*16777216 
          ) / 65536 
        ) * 65536 
      ) / 256 
    ) * 256 
  )

Where:

  uintIpAddress = 172572800

result: “10.73.64.128”

IPv4 Network Segment Manipulation Functions:

Note that (^) represents raising the left numeral to the right power and INT(fn(..)) is being used to round off the decimal.

IPv4 Network Isolation

Effectively, we’re doing a bitwise right shift 15 positions (integer division) and a left shift 15 positions (multiplication) to emulate an XOR.

uint ipv4_network(uint uintIpAddress, uint uintMaskLength) =

  INT(uintIpAddress / 2^(32 - uintMaskLength)) * 2^(32 - uintMaskLength) 
  
Where: 

  uintIpAddress = 172572800 
  uintMaskLength = 17 
  
result: 172556288 (10.73.0.0)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.