# Ip address to long number and visa versa

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
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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 Commented:
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
Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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
Commented:
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 Commented:
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
Commented:
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
Commented:
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
Commented:
if you looked at the code you would see that the ip address being tested is 45.30.20.255
0
Author Commented:
Thanks zsi,
Yeah, u were right as i could not test it y'day but now it is correct. Thanks for the help.

0
Commented:
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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.