<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>WebDH blog - PHP</title>
			<link>http://blog.webdh.com/index.cfm</link>
			<description>This is Troy&apos;s blog for WebDH.com LLC.</description>
			<language>en-us</language>
			<pubDate>Mon, 06 Sep 2010 13:02:58 -0700</pubDate>
			<lastBuildDate>Tue, 06 Jan 2009 11:57:00 -0700</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>webdh.com@gmail.com</managingEditor>
			<webMaster>webdh.com@gmail.com</webMaster>
			
			<item>
				<title>ColdFusion UDFs long2ip() and ip2long()</title>
				<link>http://blog.webdh.com/index.cfm/2009/1/6/ColdFusion-UDFs-long2ip-and-ip2long</link>
				<description>
				
				Yesterday I was working on porting some PHP code into ColdFusion. The PHP code was using a function called long2ip(), and I researched this &lt;a href=&quot;http://us3.php.net/manual/en/function.long2ip.php&quot;&gt;PHP Manual&lt;/a&gt; website to learn more about it.  I needed this function in CF, but couldn&apos;t find it at cflib or anywhere else doing a few Google searches. The closest I came was this &lt;a href=&quot;http://www.bpurcell.org/blog/index.cfm?mode=entry&amp;entry=1078&quot;&gt;blog post by Brandon Purcell&lt;/a&gt;, which got me started in the right direction. I learned from what Brandon wrote, along with the comments input by Gabriel Malca (if the function doesn&apos;t exist) at the PHP Manual site and came up with the logic for my CF version of the function.  After I had this working, I figured I better implement the opposite conversion function as well, so I also created ip2long(). Again I found a user comment for when the function doesn&apos;t exist to base my function&apos;s logic. I submitted these to &lt;a href=&quot;http://www.cflib.org&quot;&gt;cflib.org&lt;/a&gt; today, so look for them soon.  

Examples:&lt;br&gt;
long2ip(3401190660) = 202.186.13.4&lt;br&gt;
ip2long(202.186.13.4) = 3401190660

Here is the code for UDFs below:
&lt;code&gt;
&lt;cfscript&gt;
/**
 * Generates an (IPv4) Internet Protocol dotted address (aaa.bbb.ccc.ddd) from the proper address representation. Returns 0 if error occurs.
 * 
 * @param longip Numeric value of the address you want to convert. (Required)
 * @return Returns a String. 
 * @author Troy Pullis (tpullis@yahoo.com) 
 * @version 1, Jan 5, 2009 
 */
function long2ip(longip)
{
	var ip = &quot;&quot;;
	var i = &quot;&quot;;
    if (longip &lt; 0 || longip &gt; 4294967295) 
		return 0;
    for (i=3;i&gt;=0;i--) {
        ip = ip &amp; int(longip / 256^i);
        longip = longip - int(longip / 256^i) * 256^i;
        if (i&gt;0) 
			ip = ip &amp; &quot;.&quot;;
    }
    return ip;
}

/**
 * Converts a string containing an (IPv4) Internet Protocol dotted address (aaa.bbb.ccc.ddd) into a proper address representation.  Returns 0 if error occurs.
 * 
 * @param ip Dotted address value you want to convert. (Required)
 * @return Returns a String. 
 * @author Troy Pullis (tpullis@yahoo.com) 
 * @version 1, Jan 5, 2009 
 */
function ip2long(ip) {
	var iparr = ListToArray(ip,&quot;.&quot;);
	if (ArrayLen(iparr) != 4)
		return 0;
	else 
	 	return iparr[1]*256^3 + iparr[2]*256^2 + iparr[3]*256 + iparr[4];
}
&lt;/cfscript&gt;
&lt;/code&gt; 
				</description>
				
				<category>PHP</category>				
				
				<category>ColdFusion</category>				
				
				<pubDate>Tue, 06 Jan 2009 11:57:00 -0700</pubDate>
				<guid>http://blog.webdh.com/index.cfm/2009/1/6/ColdFusion-UDFs-long2ip-and-ip2long</guid>
				
			</item>
			</channel></rss>