Ip address to long number and visa versa

Posted on 1998-04-28
Last Modified: 2012-06-21
I hv an ip address list ( for ex. , 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
    i5 = i1
End If

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

i3 = iConsume(Address)
If i3 > 128 Then
    i7 = i3 - 256
    i7 = i3
End If

i4 = iConsume(Address)
If i4 > 128 Then
    i8 = i4 - 256
    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 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,
Question by:nikhil_vora
  • 4
  • 3
  • 2
  • +1

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)

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,

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

' Somewhere in your code, for an IP address of

Dim x as tBytes
Dim y as tLong
Dim MyAddressAsLong as Long

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

LSet xLong = xByte

MyAddressAsLong = xLong.MyLong


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.

Expert Comment

ID: 1432957

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.


   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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Author Comment

ID: 1432958
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..

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.

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.

Expert Comment

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

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.


Expert Comment

ID: 1432963

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.



Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now