将IP地址转换为长整型、将长整型转换为IP地址

将IP地址转换为长整型
Convertsastringipaddress("192.168.0.1")toaLongnumber(3232235521).OneofthereasonstodothiswouldbetostoreIPaddressesindatabases.Numbersgreatlyreducethesizerequiredtostorethisinformation.
Inputs:asNewIP-StringIPaddress("192.168.0.1")toconverttoanumber.
Returns:ReturnsaLongIntegerrepresentingtheIPaddress(3232235521)
Assumes:ThisfunctionassumesthatyourIPaddresshas4integersdelimitedbydecimalsandthatthenumbersrangefrom0to255.
FunctionCLngIP(ByValasNewIP)
DimlnResults
DimlnIndex
DimlnIpAry
’SplittheIPaddressusingthedotasadelimiter
lnIpAry=Split(asNewIP,".",4)
’LoopthroughEachnumberIntheIPaddress
ForlnIndex=0To3
’IfweareNotworkingWiththelastnumber...
IfNotlnIndex=3Then
’ConvertthenumberToavaluerangethatcanbeparsedfromtheothers
lnIpAry(lnIndex)=lnIpAry(lnIndex)*(256^(3-lnIndex))
EndIf
’AddthenumberTotheresults
lnResults=lnResults+lnIpAry(lnIndex)
Next
’IfstoringnumberwithinanAccessDatabase,
’ThevariableType"Long"rangesfrom-2147483648To2147483647
’YouwillneedTosubtract2147483648fromthenumber
’beforequeryingthedatabase.
’lnResults=lnResults-2147483648
’Returntheresults
CLngIP=lnResults
EndFunction
将长整型转换为IP地址
Name:ConvertLongIPtoStringIP
Description:ThisfunctionconvertsaLongNumber(3232235521)intoanIPAddress("192.168.0.1").Whywouldyouwanttodothis?Clickhere.
Inputs:anNewIP-IPAddressasaLongNumber(nodots)
Returns:ReturnsthestringrepresentationofanIPAddress("192.168.0.1")
FunctionCStrIP(ByValanNewIP)
DimlsResults’ResultsTobereturned
DimlnTemp’Temporaryvaluebeingparsed
DimlnIndex’Positionofnumberbeingparsed
’IfpullingnumberfromanAccessDatabase,
’ThevariableType"Long"rangesfrom-2147483648To2147483647
’YouwillfirstneedToadd2147483648tothenumbertoparsecorrectly.
’anNewIP=anNewIP+2147483648
’Parsehighestnumbersfirst
ForlnIndex=3To0Step-1
’ParsethecurrentvalueForthisposition
lnTemp=Int(anNewIP/(256^lnIndex))
’AppendthenumberTothefinalresultsdelimitedbyadot
lsResults=lsResults&lnTemp&"."
’Removethenumberthatwejustparsed
anNewIP=anNewIP-(lnTemp*(256^lnIndex))
Next
’Cutofflastdot
lsResults=Left(lsResults,Len(lsResults)-1)
’Returntheresults
CStrIP=lsResults
EndFunction

Copyright © 2014 ASP技巧教程源码大全 All Rights Reserved
晋ICP备14006235号-3 页面执行时间:93.750毫秒 返回顶部