# IP to Decimal

## Recommended Posts

Hi,

i hope you can help me :-) i need for a projekt a ip in decimal

http://www.allredroster.com/iptodec.htm

so i need a function who do the same as the website. i search for a while but i´m not so good in math... can you help me?

bjoerni

##### Share on other sites

```\$sIP = '192.168.4.242'

MsgBox(0, '', _IPStrToDec(\$sIP))

Func _IPStrToDec(\$sIP)

Local \$tDWord, \$Val = StringSplit(\$sIP, '.')

If \$Val[0] < 4 Then
Return SetError(1, 0, '')
EndIf
\$Val = BitShift(\$Val[1], -24) + BitShift(\$Val[2], -16) + BitShift(\$Val[3], -8) + \$Val[4]
\$tDWord = DllStructCreate('dword')
DllStructSetData(\$tDWord, 1, \$Val)
Return DllStructGetData(\$tDWord, 1)
EndFunc   ;==>_IPStrToDec```

##### Share on other sites

Another example.

```\$dec = _IPToDec("10.1.1.1")
msgbox(0,"",\$dec)

Func _IPToDec(\$ip)
\$octets = StringRegExp(\$ip,"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b",1)
If UBound(\$octets) <> 4 Then Return 0
Return Dec(Hex(Int(\$octets[0]),2) & Hex(Int(\$octets[1]),2) & Hex(Int(\$octets[2]),2) & Hex(Int(\$octets[3]),2))
EndFunc```

edit:

My math may be less efficient, but the regex validates an ip address instead of simply checking for "." separators.

Edited by spudw2k

Spoiler

Misc Code Snippets:
Projects: SubnetCalc
Cool Stuff:

##### Share on other sites

thx mans!!!!

but... i have no plan from this function.. how i make it reverse?

Edited by bjoerni

##### Share on other sites

Simple:

```Func _NumberToIP(\$nIP)
;Author: Prog@ndy
Local \$aResult = DllCall('ws2_32.dll', 'str', "inet_ntoa", "ulong", \$nIP)
If @error Then Return SetError(1,0,'0.0.0.0')
Return \$aResult[0]
EndFunc
Func _IPToNumber(\$sIP)
;Author: Prog@ndy
Local \$aResult = DllCall('ws2_32.dll', 'ulong', "inet_addr", "str", \$sIP)
If @error Then Return SetError(1,0,0)
Return \$aResult[0]
EndFunc

\$IP = "123.456.789.321"
\$Num = _IPToNumber(\$IP)
\$rec = _NumberToIP(\$Num)
MsgBox(0, '', \$IP & @CRLF & \$Num & @CRLF & \$rec)```
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

##### Share on other sites

Thats false

```\$IP = "123.100.100.100"
\$IP2 = "123.100.100.101"
\$Num = _IPToNumber(\$IP)
\$rec = _IPToNumber(\$IP2)
MsgBox(0, '', \$IP & @CRLF & \$Num & @CRLF & \$rec)```

must be:

2070176868

2070176869

but it is a other number...

##### Share on other sites

No, its correct but the return-value is network byte order wich is different from host byte order on Windows. One solution is:

```Func _NumberToIP(\$nIP, \$fHostByteOrder=True)
;Author: Prog@ndy
If \$fHostByteOrder Then
Local \$aResult = DllCall('ws2_32.dll', 'ulong', "ntohl", "ulong", \$nIP)
If @error Then Return SetError(2,0,0)
\$nIP = \$aResult[0]
EndIf
Local \$aResult = DllCall('ws2_32.dll', 'str', "inet_ntoa", "ulong", \$nIP)
If @error Then Return SetError(1,0,'0.0.0.0')
Return \$aResult[0]
EndFunc
Func _IPToNumber(\$sIP, \$fHostByteOrder = True)
;Author: Prog@ndy
Local \$aResult = DllCall('ws2_32.dll', 'ulong', "inet_addr", "str", \$sIP)
If @error Then Return SetError(1,0,0)
If \$fHostByteOrder Then
\$aResult = DllCall('ws2_32.dll', 'ulong', "ntohl", "ulong", \$aResult[0])
If @error Then Return SetError(2,0,0)
EndIf
Return \$aResult[0]
EndFunc```
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

##### Share on other sites

thx!! thats the right for me

##### Share on other sites

@spudw2k

Nice, but try, for example, with "192.168.4.242".

yea, those darn 32 bit signed integers. You're right.

Spoiler

Misc Code Snippets:
Projects: SubnetCalc
Cool Stuff:

## Create an account

Register a new account

×

• Wiki

• Back

• Git