Android Question convert this php code to b4a

Addo

Well-Known Member
Licensed User
Longtime User
i got this code to convert ip address to a long range


B4X:
function Dot2LongIP ($IPaddr) {
 if ($IPaddr == "")
 {
   return 0;
 } else {
   $ips = explode(".", $IPaddr);
   return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
 }
}


i have adjusted to b4a as following


B4X:
Sub dot2longip(ipaddr As String) As String
    
Dim iptowork As String
Dim iparray() As String
Dim ip1 As Int
Dim ip2 As Int
Dim ip3 As Int
Dim ip4 As Int
Dim iplongword As Long
Dim dotstoip As String
Dim iplength As Int


iptowork = ipaddr

iparray = Regex.Split("\.", iptowork)

iplength = iparray.Length

Log(iplength)

If iplength = 4 Then

ip1 = iparray(3)
ip2 = iparray(2)
ip3 = iparray(1)
ip4 = iparray(0)

iplongword = ip1 + ip2 * 256 + ip3 * 256 * 256 + ip4 * 256 * 256 * 256

dotstoip = iplongword

End If

Return dotstoip

End Sub


but the result in B4A is different from the php code

in php i got a result of

2779649888

in b4a/b4j i got

-1515317408


using that ip address 165.174.23.96
 

OliverA

Expert
Licensed User
Longtime User
Declare ip1, ip2, ip3 and ip4 as long. That should fix your calculation
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User

Modified

B4X:
    Dim ip0 As Long
    Dim ip1 As Long
    Dim ip2 As Long
    Dim ip3 As Long
 
Upvote 0
Top