Kaptain Krayola - pie 2.0
The Kaptain likes pie. The Kaptain likes the internet because it’s full of pie.

Mangoes Support Communism

Mangoes Support Communism
Archive for the 'ColdFusion' Category
ColdFusion IP to Num and Num to IP for database storage
Author: The Kaptain14.06.2007
Yet another piece of hand and useful code from the Kaptain. There has been a ton of discussion using this example over at Perkiset’s (AKA The Cache) so The Kaptain took a few minutes to get in on the action. You can find ASP, PHP, and a C based PHP extension at The Cache to accomplish the same thing but it’s in ColdFusion here just for fun.
Here you go!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <cfscript>
function ip2num(ip){
oct1 = listfirst(ip,'.');
oct2 = listgetat(ip,2,'.');
oct3 = listgetat(ip,3,'.');
oct4 = listlast(ip,'.');
returnThis = 1 & iif(len(oct1 LT 3),DE("#repeatstring('0',3-len(oct1))##oct1#"),DE("#oct1#")) & iif(len(oct2 LT 3),DE("#repeatstring('0',3-len(oct2))##oct2#"),DE("#oct2#")) & iif(len(oct3 LT 3),DE("#repeatstring('0',3-len(oct3))##oct3#"),DE("#oct3#")) & iif(len(oct4 LT 3),DE("#repeatstring('0',3-len(oct4))##oct4#"),DE("#oct4#")) ;
return returnThis;
}
function num2ip(num){
num = removechars(num,1,1);
oct1 = left(num,3);
oct2 = mid(num,4,3);
oct3 = mid(num,7,3);
oct4 = right(num,3);
returnThis = "";
for(i = 1; i LTE 4; i = i + 1){
currOct = evaluate("oct#i#");
for(z = 1; z LTE 3; z = z + 1){
if(left(currOct,1) EQ 0){
currOct = removechars(currOct,1,1);
}else{
if(i LT 4){
returnThis = returnThis & currOct & '.';
}else{
returnThis = returnThis & currOct;
}
writeOutput(currOct & "<br>");
break;
}
}
}
return returnThis;
}
</cfscript>
|
Pretty simple eh? Here is the usage:
1 2 3 4 5 6 7 | <cfoutput> <cfset ipnum = ip2num("64.168.11.1")> <cfset numip = num2ip(ipnum)> #ipnum# - #numip# </cfoutput> |
The output:
Original IP : 64.168.11.1
ip2num: 1064168011001
num2ip: 64.168.11.1
Enjoy!
read comments (0)















