Jump to content

IP to Decimal


bjoerni
 Share

Recommended Posts

$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

Link to comment
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
Link to comment
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

Link to comment
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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...