Skip to content

Instantly share code, notes, and snippets.

@timcunningham
Last active November 4, 2021 17:12
Show Gist options
  • Save timcunningham/115ac64c8e3f04f15b191016c0eb0b52 to your computer and use it in GitHub Desktop.
Save timcunningham/115ac64c8e3f04f15b191016c0eb0b52 to your computer and use it in GitHub Desktop.
Converting IP Addresses To Integer Values With ColdFusion
<!--- Set the IP address. --->
<cfset ipAddress = "10.150.160.228" />
<!--- Break the IP address into numeric units. --->
<cfset ipParts = listToArray( ipAddress, "." ) />
<!--- Create a running total for our numeric IP equivalent. --->
<cfset ipNumber = 0 />
<!---
Loop over the parts of the array. For each part, we are going
to let the 8 bits be continually shifted over to add exclusive
bits to the running total.
--->
<cfloop
index="offset"
from="1"
to="#arrayLen( ipParts )#"
step="1">
<!---
Since each IP unit is a max of 255, we need to shift it
over (bit-wise) for multiples of 8.
--->
<cfset ipNumber += bitSHLN(
ipParts[ offset ],
((arrayLen( ipParts ) - offset) * 8)
) />
</cfloop>
<!--- Output the resultant IP number equivalent. --->
<cfoutput>
IP Address: #ipAddress#<br />
IP Number: #ipNumber#
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment