I looked but maybe I don't know what search term to look for.
I need to convert an IP address (WWW.XXX.YYY.ZZZ) to a long. How???
TIA and cheers.
I need to convert an IP address (WWW.XXX.YYY.ZZZ) to a long. How???
TIA and cheers.
DIM lIP As Long DIM IPAddr As Byte Ptr '---Fill the lIP value in whatever way IPAddr = VarPtr(lIP) Function = Using$("#_.#_.#_.#", @IPAddr, @IPAddr[1], @IPAddr[2], @IPAddr[3])
DIM sIP AS STRING DIM lIP AS LONG '---Fill the sIP value in whatever way '---Supposing sIP is in the form x.y.z.w lIP = ((Val(Parse$(sIP, ".", 4))*256+Val(Parse$(sIP, ".", 3)))*256+Val(Parse$(sIP, ".", 2)))*256+Val(Parse$(sIP, ".", 1))
#INCLUDE "WIN32API.INC" Declare Function inet_addr LIB "wsock32.dll" Alias "inet_addr" (cp As Asciiz) AS Dword ' Dotted to numeric Declare Function inet_ntoa LIB "wsock32.dll" Alias "inet_ntoa" (byval inn As Dword) As Dword ' returns Asciiz Ptr Function PbMain() Dim szIP As Asciiz * 16 Dim pszIP As Asciiz Ptr Dim IpAddr As Dword Dim IpName As String szIP = "192.168.0.100" IpAddr = inet_addr (szIP) MsgBox "Dotted IP Add as long: " + Str$(IpAddr), ,szIP pszIP = inet_ntoa (IpAddr) MsgBox "Long IP Add as dotted: " + @pszIP, ,Str$(IpAddr) HOST ADDR "Localhost" To IpAddr ' PB Statement HOST NAME IpAddr To IpName ' PB Statement pszIP = inet_ntoa (IpAddr) MsgBox "HOST ADDR: " + str$(IpAddr) + $CR + _ "HOST NAME: " + IpName + $CR + _ "Dotted IP: " + @pszIP End Function
DECLARE SUB pause ' Longs can go from -2,147,483,648 to +2,147,483,647 FUNCTION PBMAIN() DIM lIP AS LONG DIM ip AS LONG DIM test2 AS LONG DIM IPAddr AS BYTE PTR DIM sip AS STRING LOCAL start AS LONG LOCAL finish AS LONG start = 1677830336 ' <----------Start of my home network. Adjust as you see fit. finish = start + 10 FOR ip = start TO finish an$ = INKEY$ IF an$ = CHR$(27) THEN EXIT FOR IF an$ = CHR$(32) THEN pause ' long to string IPAddr = VARPTR(ip) sip = USING$("#_.#_.#_.#", @IPaddr, @IPaddr[1], @IPaddr[2], @IPaddr[3]) HOST NAME (ip) TO hostname$ ' string 2 long test2 = ((VAL(PARSE$(sIP, ".", 4))*256+VAL(PARSE$(sIP, ".", 3)))*256+VAL(PARSE$(sIP, ".", 2)))*256+VAL(PARSE$(sIP, ".", 1)) PRINT;ip,sip,hostname$ NEXT ipaddr BEEP WAITKEY$ END FUNCTION SUB pause WHILE INSTAT = 0 : WEND an$ = INKEY$ END SUB
ipLong = CVL(CHR$(ZZZ,YYY,XXX,WWW))
ipLong = CVL(CHR$(WWW,XXX,YYY,ZZZ))
'================================================== ' String2IP - converts a text IP address like 10.145.222.10 to a DWORD value (which is how windows handles them internally) '================================================== Function String2IP(sIP As String) As Dword Local dIP As Dword Local pIP As Byte Ptr If ParseCount(sIP,".") > 2 Then pIP = VarPtr(dIP) 'assign the Byte size pointer to the DWORD variable called dIP (a DWORD can fit 4 byte variables inside it) If pIP <> 0 Then @pIP = Val(Parse$(sIP,".",1)) 'assign the 1st octet of the dword variable called dIP @pIP[1] = Val(Parse$(sIP,".",2)) '2nd octet (the [1] tells it we want to offset the pointer by one byte size variable) @pIP[2] = Val(Parse$(sIP,".",3)) '3rd octet @pIP[3] = Val(Parse$(sIP,".",4)) '4th octet Function = dIP End If Else Function = 0 End If End Function '================================================== ' IP2String - converts a DWORD in to the text version of a IP address (opposite of the String2IP function) '================================================== Function IP2String(dRawAddr As Dword) As String Dim pIP As Byte Ptr pIP = VarPtr(dRawAddr) 'assign the address to the pointer If pIP <> 0 Then Function = Using$("#_.#_.#_.#", @pIP, @pIP[1], @pIP[2], @pIP[3]) 'the powerbasic using$ command is similar to the Format() command End If End Function '================================================== ' IsValidIP - looks at a text to see if it is a valid IP address '================================================== Function IsValidIP(ByVal sIP As String) As Long Local iCount As Long If IsTrue Verify(sIP, "0123456789.") Then Exit Function 'the powerbasic Verify() command tells us if we have any character not in our list If InStr(1,sIP,"..") Then Exit Function 'not valid if we have 2 periods together If ParseCount(sIP, ".") <> 4 Then Exit Function 'not valid if we dont have exactly 4 segments For iCount = 1 To 4 'now loop through each segment and make sure the number value is from 0 to 255 If Val(Parse$(sIP, ".", iCount)) > 255 Then Exit Function Next iCount Function = %TRUE 'if we made it passed all the tests it must be valid so return TRUE End Function '================================================== ' IncrIpRange - used to process through a IP range (changes the IP dword to the next IP and returns FALSE if past the last IP in the range) '================================================== Function IncrIpRange(IP As Dword, ToIP As Dword) As Long 'increases to the next IP address Local pIP As Byte Ptr Local pToIP As Byte Ptr Local iRet As Long pIP = VarPtr(IP) pToIP = VarPtr(ToIP) If (pIP = 0) Or (pToIP = 0) Then Exit Function Incr @pIP[3] '4th octet If @pIP[3] = 0 Then Incr @pIP[2] '3rd octet If @pIP[2] = 0 Then Incr @pIP[1] '2nd octet If @pIP[1] = 0 Then Incr @pIP '1st octet End If End If End If iRet = %TRUE '%TRUE unless we find otherwise If @pIP[3] > @pToIP[3] Then iRet = %FALSE '4th octet is greater so %FALSE If @pIP[2] < @pToIP[2] Then iRet = %TRUE '3rd octet is lessthan so %TRUE even if 4th octet is greater If @pIP[2] > @pToIP[2] Then iRet = %FALSE '3rd octet is greater so %FALSE If @pIP[1] < @pToIP[1] Then iRet = %TRUE '2nd octet is lessthan so %TRUE even if 3rd or 4th octet is greater If @pIP[1] > @pToIP[1] Then iRet = %FALSE '2nd octet is greater so %FALSE If @pIP < @pToIP Then iRet = %TRUE '1st octet is lessthan so %TRUE even if 2nd or 3rd or 4th octet is greater If @pIP > @pToIP Then iRet = %FALSE '1st octet is greater so %FALSE Function = iRet 'returns %FALSE if new IP is past ToIP End Function '================================================== '================================================== ' InIpRange - tests to see if a IP is inside a IP range '================================================== Function InIpRange(dIP As Dword, dFromIP As Dword, dToIP As Dword) As Long Local pIP As Byte Ptr Local pFromIP As Byte Ptr Local pToIP As Byte Ptr Local iRet1 As Long Local iRet2 As Long pIP = VarPtr(dIP) pFromIP = VarPtr(dFromIP) pToIP = VarPtr(dToIP) If (pIP = 0) Or (pToIP = 0) Or (pFromIP = 0) Then Exit Function 'check if IP is above the From IP iRet1 = %TRUE '%TRUE unless we find otherwise If @pIP[3] < @pFromIP[3] Then iRet1 = %FALSE '4th octet is lessthan so %FALSE If @pIP[2] > @pFromIP[2] Then iRet1 = %TRUE '3rd octet is greater so %TRUE even if 4th octet is less If @pIP[2] < @pFromIP[2] Then iRet1 = %FALSE '3rd octet is lessthan so %FALSE If @pIP[1] > @pFromIP[1] Then iRet1 = %TRUE '2nd octet is greater so %TRUE even if 3rd or 4th octet is less If @pIP[1] < @pFromIP[1] Then iRet1 = %FALSE '2nd octet is lessthan so %FALSE If @pIP > @pFromIP Then iRet1 = %TRUE '1st octet is greater so %TRUE even if 2nd or 3rd or 4th octet is less If @pIP < @pFromIP Then iRet1 = %FALSE '1st octet is lessthan so %FALSE 'check if IP is below the To IP iRet2 = %TRUE '%TRUE unless we find otherwise If @pIP[3] > @pToIP[3] Then iRet2 = %FALSE '4th octet is greater so %FALSE If @pIP[2] < @pToIP[2] Then iRet2 = %TRUE '3rd octet is lessthan so %TRUE even if 4th octet is greater If @pIP[2] > @pToIP[2] Then iRet2 = %FALSE '3rd octet is greater so %FALSE If @pIP[1] < @pToIP[1] Then iRet2 = %TRUE '2nd octet is lessthan so %TRUE even if 3rd or 4th octet is greater If @pIP[1] > @pToIP[1] Then iRet2 = %FALSE '2nd octet is greater so %FALSE If @pIP < @pToIP Then iRet2 = %TRUE '1st octet is lessthan so %TRUE even if 2nd or 3rd or 4th octet is greater If @pIP > @pToIP Then iRet2 = %FALSE '1st octet is greater so %FALSE If (iRet1 = %TRUE) And (iRet2 = %TRUE) Then Function = %TRUE End Function
Do 'Process dIpFrom address '... If IsFalse IncrIPRange(dIpFrom, dIpTo) Then Exit Loop 'increases dIpFrom until it passes dIpTo Loop
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment