Dezimal in Binär umwandeln _DezToBin

  • Ganze Dezimalzahlen bis 1023 werden in Binär umgewandelt und als Array mit 10 Elementen zurückgegeben.
    Habs für meine Binäruhr gebastelt...

    [autoit]


    #cs ----------------------------------------------------------------------------
    AutoIt Version: 3.3.0.0
    Author: Hacky-Sack
    Script Function: converts round dezimalnumbers up to 1023 into bin
    Beispiel: $bin = _DezToBin(3)
    Beispiel: Returns an array with 10 elements
    #ce ----------------------------------------------------------------------------

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

    #include <array.au3>
    Func _DezToBin($query)
    DIM $return_array[10]
    If $query - 512 > -1 Then
    $return_array[9] = 1
    $query = $query - 512
    Else
    $return_array[9] = 0
    EndIf
    If $query - 256 > -1 Then
    $return_array[8] = 1
    $query = $query - 256
    Else
    $return_array[8] = 0
    EndIf
    If $query - 128 > -1 Then
    $return_array[7] = 1
    $query = $query - 128
    Else
    $return_array[7] = 0
    EndIf
    If $query - 64 > -1 Then
    $return_array[6] = 1
    $query = $query - 64
    Else
    $return_array[6] = 0
    EndIf
    If $query - 32 > -1 Then
    $return_array[5] = 1
    $query = $query - 32
    Else
    $return_array[5] = 0
    EndIf
    If $query- 16 > -1 Then
    $return_array[4] = 1
    $query = $query - 16
    Else
    $return_array[4] = 0
    EndIf
    If $query - 8 > -1 Then
    $return_array[3] = 1
    $query = $query - 8
    Else
    $return_array[3]=0
    EndIf
    If $query - 4 > -1 Then
    $return_array[2] = 1
    $query = $query - 4
    Else
    $return_array[2] = 0
    EndIf
    If $query - 2 > -1 Then
    $return_array[1] = 1
    $query = $query - 2
    Else
    $return_array[1] = 0
    EndIf
    If $query - 1 > -1 Then
    $return_array[0] = 1
    $query = $query - 1
    Else
    $return_array[0] = 0
    EndIf
    Return $return_array
    EndFunc

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

    #cs Example

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

    $bin = _DezToBin(3)
    _ArrayDisplay($bin)

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

    #ce

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

    #cs Example

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

    $bin = _DezToBin(7)
    _ArrayDisplay($bin)

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

    #ce

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

    #cs Example

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

    $bin = _DezToBin(266)
    _ArrayDisplay($bin)

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

    #ce

    [/autoit]

    ... wasweisichdennschon...

    • Offizieller Beitrag

    Naja, etwas aufwendig und nur begrenzter Zahlenbereich. ;)
    Mit der Modulo-Methode in einer Schleife reduziert sich das auf ein paar Codezeilen:

    [autoit]

    Func _IntToBin($Value)
    $bin = ''
    Do
    $bin = Mod($Value, 2) & $bin
    $Value = Floor($Value/2)
    Until $Value = 0
    Return $bin
    EndFunc

    [/autoit]
  • Hi,
    passt dazu^^

    Spoiler anzeigen
    [autoit]

    ;binary2ascii
    $a = "010011000011000001100011001101000110110001101000001100000111001101010100"
    $b = "010001110110010101100010011101010111001001110100011100110111010001100001011001110"
    $c = "www.autoit.de"

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

    MsgBox(0, $a, bin2ascii($a))
    MsgBox(0, $b, bin2ascii($b))
    MsgBox(0, $c, ascii2bin($c))
    Exit

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

    Func bin2ascii($bin_string)
    $step = 8 ;8-Bit ASCII Buchstaben
    $ascii_string = "" ;Rückgabestring
    For $f = 1 To StringLen($bin_string) Step $step ;string von Vorne nach hinten 8-bitweise durchsuchen
    $t = StringMid($bin_string, $f, $step) ; 8-Bit-Wort, ein ASCII-Buchstabe
    $bin = 0 ;startwert für
    For $i = 1 To $step ;jedes Bit suchen
    If StringMid($t, $i, 1) = "1" Then $bin += (2 ^ ($step - $i)) ;wenn Bit=1 dann binärzahl=binärzahl+2^(8-Bitposition)
    Next
    $ascii_string &= Chr($bin)
    Next
    Return $ascii_string
    EndFunc ;==>bin2ascii

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

    func ascii2bin($ascii_string)
    $step=8 ;8-Bit ASCII Buchstaben
    $bin_string=""
    for $t=1 to stringlen($ascii_string) ;jeden Buchstaben im String
    $asciicode=asc(stringmid($ascii_string,$t,1)) ;in asciicode umwandeln
    for $i=7 to 0 step -1 ;asciicode in bits
    if bitand($asciicode,2^$i) then
    $bin_string&="1"
    else
    $bin_string&="0"
    endif
    next
    next
    return $bin_string
    endfunc

    [/autoit]

    BugFix
    daß du die schweißtreibende Arbeit anderer Leute immer so niedermachen musst ;)

    ciao
    Andy

    • Offizieller Beitrag

    Aber da wir grad dabei sind - hier etwas, was die meisten Taschenrechner nicht können: FloatToBin.

    [autoit]

    Func _FloatToBin($Value, $decimal_places=6)
    Local $bin = '', $ret, $count = -1, $n = 50
    Do
    $n -= 1
    $divisor = 2^$n
    Until $divisor <= $Value
    Do
    $ret = Floor($Value/$divisor)
    $bin &= $ret
    If $ret = 1 Then $Value -= $divisor
    $divisor /= 2
    If Int($divisor) = 0 Then $count += 1
    If $count = 0 Then $bin &= ','
    Until $count = $decimal_places
    Return $bin
    EndFunc

    [/autoit]
  • Zitat

    was die meisten Taschenrechner nicht können


    Wenn du jetzt noch die Anzahl der Dezimalstellen korrigierst, dann ruf ich morgen bei HP an und lass die Funktion posthum ins ROM vom 48e patchen :rofl:
    Btw. für den HP48E gabs mal nen tollen Emulator für meinen Nokia Communicator 9210....wenn einem das damals einer gesagt hätte, daß mal nen Taschenrechner im Emulator auf nem Telefon läuft, dann wäre ich bei der umgekehrt polnischen Notation nicht so verzweifelt^^

  • Zitat

    Ihr seid doch alle Muh-Kühe..


    Du weißt doch: "Eine Kuh macht Muh und viele Kühe machen Mühe" :D

    Habe mal so etwas ähnliches gemacht, nur das das Ergebnis als String zurückgegeben wird.
    Funktioniert also ähnlich wie Hex ...

    Spoiler anzeigen
    [autoit]

    ;««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    ; BitString.au3 -- Gibt den Wert eines Integers als String im Dual-Zahlensystem zurück
    ;««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

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

    $test = 0xf1

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

    ConsoleWrite ("--- "&BitString ($test) & @crlf)

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

    Func BitString ($value)

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

    Local $sBinary = ''
    Local $ii = 0


    For $i = 31 To 0 Step - 1

    If (BitShift (BitShift ($value, -$i), $i + $ii)) Then
    $sBinary = '1' & $sBinary
    Else
    $sBinary = '0' & $sBinary
    EndIf

    $ii += 1
    Next

    Return $sBinary

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

    EndFunc

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

    Exit (0)

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


    Gruß
    Greenhorn


    2 Mal editiert, zuletzt von Greenhorn (8. September 2009 um 21:57)