首先我們要先了解一下IP地址轉(zhuǎn)換為整型(嚴(yán)格來說應(yīng)該說是長整型)的原理~
成都創(chuàng)新互聯(lián)公司是一家以網(wǎng)站建設(shè)公司、網(wǎng)頁設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、營銷推廣、小程序App開發(fā)等移動(dòng)開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為成都混凝土攪拌機(jī)等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
【轉(zhuǎn)換原理】 假設(shè)IP為 w x y z 則IP地址轉(zhuǎn)為整型數(shù)字的計(jì)算公式為 intIP = * * *w + * *x + *y + z
【PHP的互轉(zhuǎn)】 PHP的轉(zhuǎn)換方式比較簡單 它內(nèi)置了兩個(gè)函數(shù) int ip long ( string $ip_address )和 string long ip ( string $proper_address ) 可以直接調(diào)用使用~
【Asp的互轉(zhuǎn)】 自定義函數(shù)如下 |? describtion: 將IP轉(zhuǎn)換為int型數(shù)字?????????????????????????? | |????? Authors: abandonship| ~ ~ Function IP Num(ByVal strIP) ??? Dim nIP ??? Dim nIndex ??? Dim arrIP ??? arrIP = Split(strIP " " ) ??? For nIndex = To ??????? If Not nIndex = Then ??????????? arrIP(nIndex) = arrIP(nIndex) * ( ^ ( nIndex)) ??????? End If ??????? nIP = nIP + arrIP(nIndex) ??? Next ??? IP Num = nIP End Function |? describtion: 將int型數(shù)字轉(zhuǎn)換為IP?????????????????????????? | |????? Authors: abandonship| ~ ~ Function Num IP(ByVal nIP) ??? Dim strIP ??? Dim nTemp ??? Dim nIndex ??? For nIndex = To Step ??? ?nTemp = Int(nIP / ( ^ nIndex)) ??? ?strIP = strIP nTemp " " ??? ?nIP = nIP (nTemp * ( ^ nIndex)) ??? Next ??? strIP = Left(strIP Len(strIP) ) ??? Num IP = strIP End Function
【MsSQL的互轉(zhuǎn)】 自定義函數(shù)如下 /*************************************************************** ?*?將IP轉(zhuǎn)換為int型數(shù)字???????????????????????? | ?*?Code CreateBy abandonship| ?**************************************************************/ CREATE FUNCTION [dbo] [ipToInt](?? ?@strIp varchar( )?? )RETURNS bigint?? AS?? BEGIN?? ?declare @nIp bigint?? ?set @nIp = ??? ?select ??@nIp = @nIp + LEFT( @strIp charindex( @strIp+ ) )*Id? ?from(?? ??select Id = cast( * * * as bigint)?? ??union all select * * ?? ??union all select * ?? ??union all select ?) as T ?return (@nIp) END? /*************************************************************** ?*?將int型數(shù)字轉(zhuǎn)換為IP???????????????????????? | ?*?Code CreateBy abandonship| ?**************************************************************/ CREATE FUNCTION [dbo] [intToIP]( ?@nIp bigint?? )RETURNS varchar( )?? As?? BEGIN?? ?declare @strIp varchar( )?? ?set @strIp = ?? ?select ??@strIp = @strIp + + cast(@nIp/ID as varchar) @nIp = @nIp%ID ?from(?? ??select ID = cast( * * * as bigint)?? ??union all select * * ?? ??union all select * ?? ??union all select ?) as T?? ?return(stuff(@strIp ))?? END
lishixinzhi/Article/program/PHP/201311/21328
?php
$ip?=?$_SERVER["REMOTE_ADDR"];?//?獲取客戶端IP
$longip?=?ip2long($ip);
//?程序員通常將?IP?轉(zhuǎn)換成整形寫入數(shù)據(jù)庫
//?讀取的時(shí)候再把它轉(zhuǎn)成標(biāo)準(zhǔn)的?IP?地址
$ip?=?long2ip($longip);
echo?'pre';
print_r(array($longip?=?$ip));
?
浮點(diǎn)轉(zhuǎn)換成整型有三種函數(shù)
1、floor ( float value)??? 舍去法取整又稱向下取整,將小數(shù)部分舍去取整
?php
echo?floor(6.1);?//結(jié)果是6
echo?floor(6.9);//結(jié)果是6
?
2、ceil??進(jìn)一法取整,有小數(shù)部分則進(jìn)一位
?php
echo?ceil(6.1);?//結(jié)果是7
echo?ceil(6.9);//結(jié)果是7
?
3、round 浮點(diǎn)數(shù)進(jìn)行四舍五入
?php
echo?round(6.1);?//結(jié)果是6
echo?round(6.9);//結(jié)果是7
?
假設(shè)這個(gè)數(shù)組是$arr,
則方法如下:
for($i=0;$icount($arr);$i++)
{
$arr[$i] = intval($arr[$i]);
}
這樣子就能把字符型轉(zhuǎn)換成數(shù)字型數(shù)組