Code archives/Networking/Short Dotted IP to Integer IP Converter

This code has been declared by its author to be Public Domain code.

Download source code

Short Dotted IP to Integer IP Converter by Chroma2004
One of the functions I wrote for my XNet Multiplayer Library.
Function DotToInt%(ip$)
	off1=Instr(ip$,".")	  :ip1=Left$(ip$,off1-1)
	off2=Instr(ip$,".",off1+1):ip2=Mid$(ip$,off1+1,off2-off1-1)
	off3=Instr(ip$,".",off2+1):ip3=Mid$(ip$,off2+1,off3-off2-1)
	off4=Instr(ip$," ",off3+1):ip4=Mid$(ip$,off3+1,off4-off3-1)
	Return ip1 Shl 24 + ip2 Shl 16 + ip3 Shl 8 + ip4
End Function

Comments

_PJ_2013
I wasn't sure if anyone had done this, so was just checking whether it was worth posting what I've just made below...

I don't believe there's a need for the "slow" mid command, since the local var string IP can be corrupted in the conversion, and Blitz's automatic casting allows for string numericals to be operated on as numbers. As such, I believe the following may be a tad quicker, but probably wont make much real difference in an overall scheme of things...

Note that the Integer IP ought to be BigEndian if it is to be reversible by using "DottedIP$()" command.

Function UnDotIP(IP$)
 Local n4%
 Local Shift=8
 Local Inst=(Instr(IP,"."))
 While (Inst)
  n4=n4+Int(Left(IP,Inst)) Shl Shift
  IP=Right(IP,Len(IP)-Inst)
  Shift=Shift+8
  Inst=(Instr(IP,"."))
 Wend
 n4=n4+Int(IP) Shl (Shift)
Return n4
End Function



Code Archives Forum