IP in Binär umrechnen und umgekehrt

  • Moin,

    ich möchte ein kleines Programm zur Umrechnung von IP-Adressen erstellen. Meine Anfänge sind soweit auch in Ordnung, aber

    1. kriege ich es nicht hin, eine IP in's Binärformat umzuwandeln
    2. hat das Ganze starken optimierungsbedarf

    [autoit]

    Func calcIPv4()
    Local $ip
    Local $1, $2, $3, $4, $5, $6, $7, $8
    Local $block1, $block2, $block3, $block4
    Local $field = GUICtrlRead($Input3)

    [/autoit][autoit][/autoit][autoit]

    $1 = StringMid($field, 1, 1) * 128
    $2 = StringMid($field, 2, 1) * 64
    $3 = StringMid($field, 3, 1) * 32
    $4 = StringMid($field, 4, 1) * 16
    $5 = StringMid($field, 5, 1) * 8
    $6 = StringMid($field, 6, 1) * 4
    $7 = StringMid($field, 7, 1) * 2
    $8 = StringMid($field, 8, 1) * 1

    [/autoit][autoit][/autoit][autoit]

    $block1 = $1 + $2 + $3 + $4 + $5 + $6 + $7 + $8

    [/autoit][autoit][/autoit][autoit]

    $1 = StringMid($field, 9, 1) * 128
    $2 = StringMid($field, 10, 1) * 64
    $3 = StringMid($field, 11, 1) * 32
    $4 = StringMid($field, 12, 1) * 16
    $5 = StringMid($field, 13, 1) * 8
    $6 = StringMid($field, 14, 1) * 4
    $7 = StringMid($field, 15, 1) * 2
    $8 = StringMid($field, 16, 1) * 1

    [/autoit][autoit][/autoit][autoit]

    $block2 = $1 + $2 + $3 + $4 + $5 + $6 + $7 + $8

    [/autoit][autoit][/autoit][autoit]

    $1 = StringMid($field, 17, 1) * 128
    $2 = StringMid($field, 18, 1) * 64
    $3 = StringMid($field, 19, 1) * 32
    $4 = StringMid($field, 20, 1) * 16
    $5 = StringMid($field, 21, 1) * 8
    $6 = StringMid($field, 22, 1) * 4
    $7 = StringMid($field, 23, 1) * 2
    $8 = StringMid($field, 24, 1) * 1

    [/autoit][autoit][/autoit][autoit]

    $block3 = $1 + $2 + $3 + $4 + $5 + $6 + $7 + $8

    [/autoit][autoit][/autoit][autoit]

    $1 = StringMid($field, 25, 1) * 128
    $2 = StringMid($field, 26, 1) * 64
    $3 = StringMid($field, 27, 1) * 32
    $4 = StringMid($field, 28, 1) * 16
    $5 = StringMid($field, 29, 1) * 8
    $6 = StringMid($field, 30, 1) * 4
    $7 = StringMid($field, 31, 1) * 2
    $8 = StringMid($field, 32, 1) * 1

    [/autoit][autoit][/autoit][autoit]

    $block4 = $1 + $2 + $3 + $4 + $5 + $6 + $7 + $8

    [/autoit][autoit][/autoit][autoit]

    $ip = $block1 & "." & $block2 & "." & $block3 & "." & $block4

    [/autoit][autoit][/autoit][autoit]

    GUICtrlSetData($Input4, $ip)
    EndFunc

    [/autoit]

    Das ist die Funktion zum Umrechnen von Binärcode in eine IP. Das funktioniert auch soweit. Frage dazu: Geht das einfacher?

    [autoit]

    Func calcBinary()
    Local $field = GUICtrlRead($Input1)
    Local $binparts

    [/autoit][autoit][/autoit][autoit]

    $parts = StringSplit($field, ".")

    [/autoit][autoit][/autoit][autoit]

    For $k = 1 To $parts[0]
    $bin = ''

    [/autoit][autoit][/autoit][autoit]

    For $i = 0 To 7
    If IsInt($parts[$k]) Then
    $bin &= 0
    Else
    $bin &= 1
    EndIf

    [/autoit][autoit][/autoit][autoit]

    $parts[$k] = Floor($parts[$k]) / 2
    Next

    [/autoit][autoit][/autoit][autoit]

    $binparts &= $bin
    Next

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    GUICtrlSetData($Input2, $binparts)
    EndFunc

    [/autoit]

    Das ist die bisherige Funktion zum Umwandeln einer IP in Binärcode. Das funktioniert vom Grundprinzip her, jedoch erhalte ich falsche Ergebnisse. Frage dazu: Was mache ich falsch?

    Wäre klasse, wenn jemand eine Idee dazu hat.

    Einmal editiert, zuletzt von SoftCreatR (29. Februar 2012 um 17:24)

  • IP in Binärdarstellung
    [autoit]

    $IP = @IPAddress1
    $s_IPBinary = ""

    [/autoit] [autoit][/autoit] [autoit]

    For $i In StringSplit($IP, ".", 2)
    $s_IPBinary &= _DecToBin($i)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ConsoleWrite($IP & @TAB & $s_IPBinary & @CRLF)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _DecToBin($iD)
    ; ich glaube das stammt von Marsi
    Local $sR
    Do
    $sR = Mod($iD, 2) & $sR
    $iD = Floor($iD / 2)
    Until $iD = 0
    Return StringRight('0000000' & $sR, 8)
    EndFunc

    [/autoit]
    Binärdarstellung in IP
    [autoit]

    $s_BinIP = "11000000101010000000000010000110"
    $s_IP = ""
    For $i = 1 To StringLen($s_BinIP) Step 8
    $s_IP &= _BinToDec(StringMid($s_BinIP, $i, 8)) & "."
    Next
    $s_IP = StringTrimRight($s_IP, 1)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ConsoleWrite($s_BinIP & @TAB & $s_IP & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    Func _BinToDec($s_BinZahl)
    Local $d_Ret = 0
    For $x = 0 To StringLen($s_BinZahl)
    If Int(StringRight(StringTrimRight($s_BinZahl, $x), 1)) Then $d_Ret += 2^$x
    Next
    Return $d_Ret
    EndFunc

    [/autoit]
  • Spoiler anzeigen
    [autoit]


    MsgBox(0, 3423413, int2bin(3423413))
    MsgBox(0, 2^3, int8_2bin(2 ^ 3))
    MsgBox(0, @IPAddress1, IP2bin(@IPAddress1))

    [/autoit] [autoit][/autoit] [autoit]

    ;allgemein
    Func int2bin($integer) ;32Bit int in binärstring darstellen
    Local $bin_string = ""
    For $i = 31 To 0 Step -1 ;integer in bits
    If Mod($i + 1, 8) = 0 Then $bin_string &= " ";trenner
    $bin_string &= String(Int(BitAND($integer, 2 ^ $i) / (2 ^ $i)))
    Next
    Return $bin_string
    EndFunc ;==>int2bin

    [/autoit] [autoit][/autoit] [autoit]

    ;8bit
    Func int8_2bin($integer) ;8Bit int in binärstring darstellen
    Local $bin_string = ""
    For $i = 7 To 0 Step -1 ;integer in bits
    If Mod($i + 1, 8) = 0 Then $bin_string &= " ";trenner
    $bin_string &= String(Int(BitAND($integer, 2 ^ $i) / (2 ^ $i)))
    Next
    Return $bin_string
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    func IP2bin($IP)
    ;hier gehört das regex rein, um eine reguläre IPv4 zu erkennen^^
    local $ip_array=stringsplit($IP,".",2)
    local $binstring=""
    for $i in $ip_array
    $binstring&=int8_2bin($i)
    next
    return $binstring
    endfunc

    [/autoit]

    ciao
    Andy


    "Schlechtes Benehmen halten die Leute doch nur deswegen für eine Art Vorrecht, weil keiner ihnen aufs Maul haut." Klaus Kinski
    "Hint: Write comments after each line. So you can (better) see what your program does and what it not does. And we can see what you're thinking what your program does and we can point to the missunderstandings." A-Jay

    Wie man Fragen richtig stellt... Tutorial: Wie man Script-Fehler findet und beseitigt...X-Y-Problem

    Einmal editiert, zuletzt von Andy (28. Februar 2012 um 21:30)

  • @Mattthias: Du glaubst doch nicht, dass das dann AspirinJunkie geschrieben hätte oder???

    Schonmal getestet? Dann wüsstest du nämlich, dass da ein Hex-String rauskommt.

  • Hier meine Version:

    Spoiler anzeigen
    [autoit]


    #include-once
    #include <String.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $ip = "32.64.0.3"
    MsgBox(0, "IP Address to Binary String", "IP = " & @TAB & $ip & @LF & "Binary = " & @TAB & IPv4toBIN($ip))

    [/autoit] [autoit][/autoit] [autoit]

    Global $sBin = "10000001 111000 1010 00001111"
    MsgBox(0, "Binary String to IP Address", "Binary = " & @TAB & $sBin & @LF & "IP = " & @TAB & BINtoIPv4($sBin))

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Function Name: IPv4toBIN
    ; Description: Converts an IP v4 string to a binary string format
    ; Parameter(s): $sIP: the ip v4 address with a format "a.b.c.d"
    ; $bLen8
    ; True: output binary string length is always 8 digits
    ; False: output is truncated to minimum length
    ; Requirement(s): String.au3
    ; Return Value(s): binary string format of all octets from the ip address with a format "a b c d"
    ;Error Codes: 1: ip adress not in appropiate format
    ; 2: invalid ip token size -> must be between 0 and 255
    ; Author(s): UEZ
    ;Version: v0.99 Build 2012-02-28
    ;===============================================================================
    Func IPv4toBIN($sIP, $bLen8 = True)
    Local $aIPTokens = StringRegExp($sIP, "(\d+)\.(\d+)\.(\d+)\.(\d+)", 3)
    If @error Then Return SetError(1, 0, -1)
    Local $bin, $sBin, $i
    For $i = 0 To 3
    If Int($aIPTokens[$i]) Then
    If $aIPTokens[$i] > 255 Then Return SetError(2, 0, -1)
    While $aIPTokens[$i] > 0
    $bin &= Mod($aIPTokens[$i], 2)
    $aIPTokens[$i] = Floor($aIPTokens[$i] / 2)
    WEnd
    Else
    If $bLen8 Then
    $bin = "00000000"
    Else
    $bin = "0"
    EndIf
    EndIf
    If $bLen8 Then
    $sBin &= StringFormat("%0.8i", _StringReverse($bin)) & " "
    Else
    $sBin &= _StringReverse($bin) & " "
    EndIf
    $bin = ""
    Next
    Return StringMid($sBin, 1, StringLen($sBin) - 1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ; Function Name: BINtoIPv4
    ; Description: Converts a binary string format to an IP v4 string
    ; Parameter(s): $sBin: the binary string with format "a b c d"
    ; Requirement(s): String.au3
    ; Return Value(s): the ip address in format "a.b.c.d"
    ;Error Codes: 1: string is not a binary string
    ; 2: too much tokens
    ; Author(s): UEZ
    ;Version: v0.99 Build 2012-02-28
    ;===============================================================================
    Func BINtoIPv4($sBin) ;coded by UEZ 2012
    Local $aIPTokens = StringRegExp($sBin, "[01]{1,8}", 3)
    If @error Then Return SetError(1, 0, -1)
    If UBound($aIPTokens) <> 4 Then Return SetError(2, 0, -1)
    Local $sIP, $int, $x, $y, $i = 1, $aTmp
    For $y = 0 To 3
    $aTmp = StringSplit(_StringReverse($aIPTokens[$y]), "")
    For $x = 1 To UBound($aTmp) - 1
    $int += $aTmp[$x] * $i
    $i *= 2
    Next
    $sIP &= $int & "."
    $int = 0
    $i = 1
    $aTmp = 0
    Next
    Return StringMid($sIP, 1, StringLen($sIP) - 1)
    EndFunc

    [/autoit]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    6 Mal editiert, zuletzt von UEZ (28. Februar 2012 um 23:27)

  • Danke für eure Vorschläge.

    AspirinJunkie

    Funktioniert :)

    Andy

    Aus irgendwelchen Gründen habe ich noch Probleme mit deiner Lösung. Werde aber gleich nochmal schauen.

    UEZ

    Sieht wirklich gut aus und funktioniert. Wenn das umgekehrt genau so "einfach" wäre, wär's perfekt.

    ---

    Alles in allem gibts aber nun einige Ansätze, die mir sehr geholfen haben. Vielen Dank :)