0

I have the following VB function, converted from a German Excel macro:

Function GetChecksumme(identNr As Integer) As BitArray 
    Dim Z1 As Integer ' {Hilfsz„hler 1}
    Dim Checksumme As New BitArray(27)
    Dim DoCoZahl As Long
    Dim Result As New BitArray(6)

    DoCoZahl = identNr
    'Zeit fuer diesen Schritt starten:}
    'Checksumme berechnen:}
    'False = logisch 0, True = logisch 1}
    '1. Schritt: Das Array 'CheckSumme' mit 26 Nullen auffuellen:}
    For Z1 = 1 To 26
        Checksumme(Z1) = False
    Next Z1
    '2. Schritt: DotCode-Zahl in duale Darstellung umrechnen und}
    'links von den rechten 6 Nullen einschreiben:}
    Z1 = 21
    Do While DoCoZahl > 0
        Z1 = Z1 - 1
        If (DoCoZahl Mod 2) = 0 Then
            Checksumme(Z1) = False
        End If
        If (DoCoZahl Mod 2) = 1 Then
            Checksumme(Z1) = True
        End If
        DoCoZahl = DoCoZahl \ 2
    Loop
    '3. Schritt: Solange XOR-Operation durchfhren, bis}
    'die Check-Summe ermittelt ist:}
    Do
        'Den ersten '1er' von links suchen:}
        Z1 = 0
        Do
            Z1 = Z1 + 1
        Loop Until ((Checksumme(Z1) = True) Or (Z1 > 20))
        'Ab dieser Stelle mit Maske 1 0 0 0 1 1 1 logisch XOR-verknpfen:}
        'Bit mit 1 XOR-verknpfen -> Ergebnis ist das negierte Bit.}
        'Bit mit 0 XOR-verknpfen -> Ergebnis ist das Bit.}
        If Z1 <= 20 Then
            Checksumme(Z1) = Not Checksumme(Z1)
            Checksumme(Z1 + 4) = Not Checksumme(Z1 + 4)
            Checksumme(Z1 + 5) = Not Checksumme(Z1 + 5)
            Checksumme(Z1 + 6) = Not Checksumme(Z1 + 6)
        End If
    Loop Until Z1 >= 20
    'Zeit fr diesen Schritt stoppen:}


    Result(0) = Checksumme(26)
    Result(1) = Checksumme(25)
    Result(2) = Checksumme(24)
    Result(3) = Checksumme(23)
    Result(4) = Checksumme(22)
    Result(5) = Checksumme(21)

    Return result

End Function

I've been asked to convert it for an in house project. I started by trying to convert it to python (my preferred language), so I could better understand the calculation:

def getChecksum(identNr, checksumNr=None):
    if not 0 <= checksumNr <= 5:
        raise ValueError()

    checksum = [False] * 27
    z1 = 20
    doCoZahl = identNr

    while doCoZahl > 0:
        checksum[z1] = doCoZahl & 1 == 1
        z1 -= 1
        doCoZahl >>= 2

    while z1 > 20:
        z1 = 0
        while (not checksum[z1]) or z1 <= 20:
            z1 += 1

        if z1 <= 20:
             Checksumme[z1] = not Checksumme[Z1]
             Checksumme[z1 + 4] = not Checksumme[Z1 + 4]
             Checksumme[z1 + 5] = not Checksumme[Z1 + 5]
             Checksumme[z1 + 6] = not Checksumme[Z1 + 6]

    return tuple(checksum[-1:-7:-1])

The problem is that python returns (False, False, False, False, False, False), no matter what input I give it. For instance, in VB:

GetChecksumme | Value (VB)
--------------+-----------
1             | TTTFFF
2             | FTTTFF

There's probably a simple mistake somewhere, but I can't see it.

1 Answer 1

1

You never enter the final while loop.

z1 starts at 20, it (possibly) gets reduced in the while doCoZahl > 0: loop, so when you hit while z1 > 20: z1 is <= 20.

Sorry, I don't know VB. I can kind of read that VB code, but not well enough to offer a faithful translation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.