Solved

# Ip address to long number and visa versa

Posted on 1998-04-28
276 Views
hi,
I hv an ip address list ( for ex. 192.168.3.5 , 127.32.2.5 etc. ) , I am supposed to convert this ip addresses into  a long number and compare that number with the corrosponding number stored in MS Access Jet Database. The MS Access database field is declared as number ( Size long integer ). I suppose, VB doesn't give the ability to define unsigned long , so the numbers corrosponding to the ip addresses in my database are sometimes negative also.
Now, in my VB app. , i am taking ip address and convert this to number and then compare it. I  am getting different number by my conversion routine. The conversion routine is as follows ::
Dim i1 As Integer
Dim i2 As Integer
Dim i3 As Integer
Dim i4 As Integer

Dim i5 As Integer
Dim i6 As Integer
Dim i7 As Integer
Dim i8 As Integer

i1 = iConsume(Address) // gives me first byte i.e. 192 in i1
If i1 > 128 Then
i5 = i1 - 256
Else
i5 = i1
End If

i2 = iConsume(Address) // gives me second byte i.e. 168 in i2
If i2 > 128 Then
i6 = i2 - 256
Else
i6 = i2
End If

If i3 > 128 Then
i7 = i3 - 256
Else
i7 = i3
End If

If i4 > 128 Then
i8 = i4 - 256
Else
i8 = i4
End If

lIPtoNumber = i5 * 256 ^ 3 + i6 * 256 ^ 2 + i7 * 256 + i8

what could be wrong with this logic ?? Can somebody suggest me other mehod if someone knows ??

It is very urgent for me... The ipaddress for which i m testing is 192.168.102.2 and the conversion for it stored in
my database is -1062705662 and what i am getting with this routine is -1079482878.
Which one is correct ?? can somebody tell me....
Thanks & Regards,
Nikhil
0
Question by:nikhil_vora
• 4
• 3
• 2
• +1

LVL 6

Expert Comment

ID: 1432953
I hate to throw a wrench into your calculations, but running my own code which converts each section into it's corresponding hex number, merging it all and converting back to decimal I get the following:
192d = C0h
168d = A8h
102d = 66h
2d = 02h
C0A86602h = 3232261634d (unsigned) or -1084777987d (signed)
0

Author Comment

ID: 1432954
Sorry, but i had already given the no. which is stored in MS Access database which is
-1062705662 also from my calculation i got -1079482878 ....I m now more confused....
Also, how did u get unsigned no. ??? The easiest method , i was thinking was to use this formula...
i1*256^3+i2*256^2+i3*256+i4 ,
but it gives me overflow as it goes out of range of long no...I suppose in C, if i say signed x  and if it exceeds range then it is shown as negative ,  why it gives me runtime error in VB ??
Anybody throw some light on the issue and also suggest me proven method for converting ip addr to number ???
Thanks and regards,
Nikhil
0

LVL 2

Accepted Solution

woka earned 10 total points
ID: 1432955
Use something like the following.  In the Declarations section:

Type tBytes
Byte01 as Byte
Byte02 as Byte
Byte03 as Byte
Byte04 as Byte
End Type

Type tLong
MyLong as Long
End Type

Dim x as tBytes
Dim y as tLong

x.Byte01 = 1
x.Byte02 = 2
x.Byte03 = 3
x.Byte04 = 4

LSet xLong = xByte

0

LVL 6

Expert Comment

ID: 1432956
Using your values, if you try to assign i1*256^3+i2*256^2+i3*256+i4 to a long, yes, it will overflow.

However, if you assign it to a double it won't.
0

LVL 4

Expert Comment

ID: 1432957
nikhil,

A byte ranges in value from 0 to 255.  Trying to assign 256 to a byte will result in an overflow.  The same holds true when you try to pack four bytes into a long.

If you try the following code, using only value up to and incuding 255, then it will work.

zsi

---------------------------------------------------------------------------
Dim p1 As Byte
Dim p2 As Byte
Dim p3 As Byte
Dim p4 As Byte
Dim l As Long
Dim s As String
Dim a1 As Byte
Dim a2 As Byte
Dim a3 As Byte
Dim a4 As Byte

p1 = 45
p2 = 30
p3 = 20
p4 = 255

l = CLng("&H" & Hex(p1) & Hex(p2) & Hex(p3) & Hex(p4))

a1 = l And &HFF
a2 = (l And &HFF00&) / &H100
a3 = (l And &HFF0000) / &H10000
a4 = (l And &HFF000000) / &H1000000

Debug.Print l & "=" & a4 & "." & a3 & "." & a2 & "." & a1
0

Author Comment

ID: 1432958
Zsi,
Thanks, but this code converts the no. to ipaddress ( dot ) format, isn't it ? what we r working on is a routine which will convert my ip address into a long number, can u suggest something ?? Also, i hv given a test case ip address if u can tell me which number r u getting for that test ip address, that would be nice..
Thanks
Nikhil
0

LVL 2

Expert Comment

ID: 1432959
Are you actually trying some of the solutions that are being offered ?????

There are two solutions here that will definately work, mine & zsi's.  Either you haven't tried them or we're interpreting you question incorrectly.  Maybe you could give some clarification.
0

LVL 4

Expert Comment

ID: 1432960
I have to echo woka's confusion.  The line in my code that reads ' l = CLng("&H" & Hex(p1) & Hex(p2) & Hex(p3) & Hex(p4)) " does exactly what you just asked for.  The full code will convert a four part (is there any other kind) IP address into a long and then back again.

I think that it is pretty clear that you are either not trying the examples or do not understand the code being offered.
0

LVL 4

Expert Comment

ID: 1432961
if you looked at the code you would see that the ip address being tested is 45.30.20.255
0

Author Comment

ID: 1432962
Thanks zsi,
Yeah, u were right as i could not test it y'day but now it is correct. Thanks for the help.

0

LVL 4

Expert Comment

ID: 1432963
nikhil,

ok, since my answer was correct, how about giving me the points?  :)

You'll have to reject woka's open answer so that I can resubmit.

Thanks!

zsi
0

## Featured Post

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…