IP Number to dotted IP Address

1. Junior Member
Join Date
Dec 1969
Posts
7

## IP Number to dotted IP Address

Hello,<BR><BR>Following is a code that converts an IP Addresse into IP Number. Can anyone tell me how can I re-convert that IP number into dotted IP Address.<BR><BR>Function IPAddress2IPNumber(sdotted)<BR> Dim dotted, ip<BR> dotted = split(sdotted, ".")<BR> ip = (dotted(0) * 16777216) + (dotted(1) * 65536) + (dotted(2) * 256) + (dotted(3))<BR> IPAddress2IPNumber = ip<BR>End Function<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
1,015

## RE: IP Number to dotted IP Address

I guess the only way is to remember where the original dots where since IP adresses come in a wide variety of shapes...<BR><BR>Otherwise, you could ping every possibility and continue until you&#039;ve found the correct one...<BR>

3. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: IP Number to dotted IP Address

&#060;%<BR>Function Number2Address(ip)<BR> Dim dotted(2)<BR> dotted(0) = ip 16777216<BR> ip = ip mod 16777216<BR> dotted(1) = ip 65536<BR> ip = ip mod 65536<BR> dotted(2) = ip 256<BR> ip = ip mod 256<BR> dotted(3) = ip<BR> Number2Address = PadNumber(dotted(0)) & "." & PadNumber(dotted(1)) & "." & PadNumber(dotted(2))<BR>End Function<BR>Function PadNumber(sString)<BR> sString = Trim(sString & "")<BR> Select Case Len(sString)<BR> Case 0<BR> PadNumber = "000"<BR> Case 1<BR> PadNumber = "00" & sString<BR> Case 2<BR> PadNumber = "0" & sString<BR> Case Else<BR> PadNumber = sString<BR> End Select<BR>End Function<BR>%&#062;<BR><BR>Or something anyway.<BR><BR>Craig.

4. Junior Member
Join Date
Dec 1969
Posts
7

## RE: IP Number to dotted IP Address

After searching on the internet I have found the following PHP code that does it but I dont know how to convert it into ASP.<BR><BR>function IPNumber2IPAddress(\$number) {<BR> \$a = (\$number / 16777216) % 256;<BR> \$b = (\$number / 65536) % 256;<BR> \$c = (\$number / 256) % 256;<BR> \$d = (\$number) % 256;<BR> \$dotted = \$a.".".\$b.".".\$c.".".\$d;<BR> return \$dotted;<BR> }<BR><BR>Can anyone guide me?

5. Senior Member
Join Date
Dec 1969
Posts
16,931

## Lets try again...

&#060;% <BR>Function Number2Address(ip) <BR> Dim dotted(3) <BR> dotted(0) = ip 16777216 <BR> ip = ip mod 16777216 <BR> dotted(1) = ip 65536 <BR> ip = ip mod 65536 <BR> dotted(2) = ip 256 <BR> ip = ip mod 256 <BR> dotted(3) = ip <BR> Number2Address = PadNumber(dotted(0)) & "." & PadNumber(dotted(1)) & "." & PadNumber(dotted(2)) & "." & PadNumber(dotted(3)) <BR>End Function <BR>Function PadNumber(sString) <BR> sString = Trim(sString & "") <BR> Select Case Len(sString) <BR> Case 0 <BR> PadNumber = "000" <BR> Case 1 <BR> PadNumber = "00" & sString <BR> Case 2 <BR> PadNumber = "0" & sString <BR> Case Else <BR> PadNumber = sString <BR> End Select <BR>End Function <BR>%&#062; <BR><BR>Idiot Craig, I missed off the last number.<BR><BR>Craig.

6. Junior Member
Join Date
Dec 1969
Posts
7

## RE: IP Number to dotted IP Address

Hello Craig,<BR><BR>Thanks for your reply. I have tried your code but getting the following error.<BR><BR>Error Type:<BR>Microsoft VBScript runtime (0x800A0006)<BR>Overflow: &#039;ip&#039;<BR>/ip.asp, line 4<BR><BR>Najam

7. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: IP Number to dotted IP Address

Ok, so debug. If you think this is hard enough to put in the advanced forum, you should be able to debug a little and see what&#039;s going on, hey?<BR><BR>I&#039;m working at the moment - that code was written off the top of my head and wasn&#039;t checked. It was just to give you an idea of how to sort out the maths - although that PHP should have given you that...<BR><BR>Craig.

8. Junior Member
Join Date
Dec 1969
Posts
7

## RE: IP Number to dotted IP Address

I have converted the following code into ASP but its not working. Any clues?<BR><BR>Here is sample PHP code:<BR><BR>function IPNumber2IPAddress(\$number) {<BR> \$a = (\$number / 16777216) % 256;<BR> \$b = (\$number / 65536) % 256;<BR> \$c = (\$number / 256) % 256;<BR> \$d = (\$number) % 256;<BR> \$dotted = \$a.".".\$b.".".\$c.".".\$d;<BR> return \$dotted;<BR> }<BR><BR>Here is the code that I converted into ASP:<BR><BR>Function IPNumber2IPAddress(snumber)<BR> Dim a, b, c, d, dotted<BR> a = (snumber / 16777216) mod 256<BR> b = (snumber / 65536) mod 256<BR> c = (snumber / 256) mod 256<BR> d = (snumber) mod 256<BR> dotted = a & "." & b & "." & c & "." & d<BR> IPNumber2IPAddress = dotted<BR>End Function<BR><BR>Any ideas would be highly appreciated.<BR><BR>Thanks<BR>Najam

9. Senior Member
Join Date
Dec 1969
Posts
1,015

## what error are you getting back? (eop)

.

10. Junior Member
Join Date
Dec 1969
Posts
7

## RE: what error are you getting back?

Function IPAddress2IPNumber(sdotted)<BR> Dim dotted, ip<BR> dotted = split(sdotted, ".")<BR> ip = (dotted(0) * 16777216) + (dotted(1) * 65536) + (dotted(2) * 256) + (dotted(3))<BR> IPAddress2IPNumber = ip<BR>End Function<BR><BR>Function IPNumber2IPAddress(snumber)<BR> Dim a, b, c, d, dotted<BR> a = (snumber / 16777216) mod 256<BR> b = (snumber / 65536) mod 256<BR> c = (snumber / 256) mod 256<BR> d = (snumber) mod 256<BR> dotted = a & "." & b & "." & c & "." & d<BR> IPNumber2IPAddress = dotted<BR>End Function<BR><BR>Here is how I am doing it:<BR><BR>IPADDR = "192.168.0.1"<BR>Response.Write(IPADDR & "<BR>")<BR>Response.Write(IPAddress2IPNumber(IPADD R) & "<BR>")<BR>Response.Write(IPNumber2IPAddress(IPAdd ress2IPNumber(IPADDR)) & "<BR>")<BR><BR><BR>Here is the error I am getting<BR><BR>Error Type:<BR>Microsoft VBScript runtime (0x800A0006)<BR>Overflow: &#039;snumber&#039;<BR>/ip.asp, line 40<BR><BR><BR>Please note that if I remove the line no. 5 "d = (snumber) mod 256" from the function IPNumber2IPAddress, it returns me "193.168.0."<BR><BR>Many Thanks for your reply<BR>Najam

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•